59805

binding network interface to Apache httpclient

Question:

I am using Apache HttpClient on a machine which has two network cards. I can not find how I can bind HttpClient to use one of the NICs only. I have found some solutions but they are all depreciated now. I am using Apache HttpClient 4.5.2

Are there any examples that use GET/POST requests while using NIC binding?

Answer1:

Arya , you will have to get the list of network interfaces and use the RequestBuilder interfaces to get this accomplished. The following will give you a rough cut idea.

<pre class="lang-java prettyprint-override">import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; public static void main(String[] args) throws Exception { //Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces(); /*if (nifs == null) { System.err.println("Error getting the Network Interface"); return; }*/ //A specific network interface can be obtained using getByName NetworkInterface nif = NetworkInterface.getByName("sup0"); System.out.println("Starting to using the interface: " + nif.getName()); Enumeration<InetAddress> nifAddresses = nif.getInetAddresses(); RequestConfig config = RequestConfig.custom() .setLocalAddress(nifAddresses.nextElement()).build(); HttpGet httpGet = new HttpGet("http://localhost:8080/admin"); httpGet.setConfig(config); CloseableHttpClient httpClient = HttpClients.createDefault(); try { CloseableHttpResponse response = httpClient.execute(httpGet); try { //logic goes here } finally { response.close(); } } finally { httpClient.close(); } }

Recommend

  • How to use basic http authentication on Windows rt- Visual Studio 2012 - C#
  • How to iterate through the list of IPv4 network addresses in Java or Android one by one?
  • Getting the IP address of the android device when connected to 3G mobile network
  • .cs files can't be opened anymore in VS 2013 Professional Update 4
  • Get back the assembly level code from exe files?
  • How can I convert the decimal representation of an IP address into binary?
  • How to retrieve String from DatagramPacket [duplicate]
  • Spring Data JPA - custom @Query with “@Param Date” doesn't work
  • Java DNSLookup get DNS attributes
  • Check if DELETE key is pressed?
  • How can I find all locally bound IP addresses in Java?
  • Co-Occurre​nce Constraint​
  • Bind events to Item ViewModel
  • How do I configure other user defined data-type with Entity Framework Core?
  • Creating a custom InputScope for Windows Phone 7
  • Binding ContextMenu Tag to Owner
  • How can I get process name of specific PID with ps command in alpine
  • Connection pooling with URLConnection?
  • richtextbox to string
  • Android Database Error - getWriteableDatabase
  • Using Generics on right hand side in Java 6?
  • Retrieve IP address of device
  • What is the purpose of TaskExecutor in spring?
  • how to find common suffix in java by using method
  • OpenGL ES texture problem, 4 duplicate columns and horizontal lines (Android)
  • ImageMagick, replace semi-transparent white with opaque white
  • Cannot connect to cassandra from Spark
  • Read text file and split every line in MSBuild
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • C# - Serializing and deserializing static member
  • Get object from AWS S3 as a stream
  • Java applet as stand-alone Windows application?
  • Cross-Platform Protobuf Serialization
  • Validaiting emails with Net.Mail MailAddress
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Do I've to free mysql result after storing it?
  • How to format a variable of double type
  • Error creating VM instance in Google Compute Engine
  • coudnt use logback because of log4j
  • JaxB to read class hierarchy