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.
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, http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html, it is said that such library can support the functionality I was looking for. A discussion on this can be found at Define source ip address using Apache HttpClient. Actually, the posted thread seems not conclusive, as users' experiences are very contrasting on the described use of the library.
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.
Moreover, the problem is not faced with the use of java.net library (as in my project) at all.
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.
Therefore, I would appreciate if any users, who had POSITIVE experiences in solving this issue, can address me to a solution/idea/method.
Thanks in advance,
Marcello
UPDATE:
I've currently implemented this test code. It gives me correct status code (200). However, it needs to be tested with multiple ip addresses.
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");
}
}
}
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.
UPDATE 2: To be precise, I've included authentication setup for those who needed it.