title: JAVA: How to create http url connection selecting the ip address to use
body: <p>I have a pool of public ip addresses configured on my multiple NICs.
  In my JAVA project, which runs on a LINUX machine, I need to select a specific ip address from the pool and create an HttpURLConnecion using that ip. Further, I will cycle on the pool, using each time a different ip.</p>
  
  <p>At the current stage, I was not able to find a solution using the java.net library. I have rather looked at the HttpClient from Apache. At the following link, <a href="http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html" rel="nofollow noreferrer">http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html</a>, it is said that such library can support the functionality I was looking for. A discussion on this can be found at <a href="https://stackoverflow.com/questions/8213714/define-source-ip-address-using-apache-httpclient">Define source ip address using Apache HttpClient</a>. Actually, the posted thread seems not conclusive, as users' experiences are very contrasting on the described use of the library.</p>
  
  <p>Therefore, I don't think that SO community really succeeded in solving this issue. It is a matter of fact that several replayed questions/answers on this topic can be found on SO, but none of them seems to give an exhaustive analysis of the problem.</p>
  
  <p>Moreover, the problem is not faced with the use of java.net library (as in my project) at all.</p>
  
  <p>At the moment, a possible option that I have is to invoke some LINUX system commands (from java) to switch the NIC to use for the current connection. However, I have not figure it out yet.</p>
  
  <p>Therefore, I would appreciate if any users, who had POSITIVE experiences in solving this issue, can address me to a solution/idea/method.</p>
  
  <p>Thanks in advance,</p>
  
  <p>Marcello</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I've currently implemented this test code. It gives me correct status code (200). However, it needs to be tested with multiple ip addresses.</p>
  
  <pre><code>public class test {
  
      public static void main(String[] args) {
  
          final String authUser = "admin";
          final String authPassword = "password";
          Authenticator.setDefault(
             new Authenticator() {
                public PasswordAuthentication getPasswordAuthentication() {
                   return new PasswordAuthentication(
                         authUser, authPassword.toCharArray());
                }
             }
          );
  
          System.setProperty("http.proxyUser", authUser);
          System.setProperty("http.proxyPassword", authPassword);
  
          try {
  
              Properties systemProperties = System.getProperties();
              URL url = new URL("yourURL");
              systemProperties.setProperty("http.proxyHost","localhost");
              systemProperties.setProperty("http.proxyPort", "8080");                         
  
              HttpURLConnection connection = (HttpURLConnection) url.openConnection();
              int status = connection.getResponseCode();
              System.out.println(status);
  
          } catch (IOException e) {
              System.out.println("connection problems");
          }
      }
  
  }
  </code></pre>
  
  <p>At this point, you should be able to configure the different TCP ports related to each NIC. Did anyone try something like this?
  I am looking forward to reading new ideas/comments.</p>
  
  <p><strong>UPDATE 2:</strong>
  To be precise, I've included authentication setup for those who needed it.</p>