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.
Why don't you just use org.apache.httpcomponents?
Here an example that works (using maven plugin org.apache.httpcomponents, version 4.3.1):
java.net
usessun.net.www.protocol
package to createHttpURLConnection
.sun.net.www.protocol.HttpURLConnection
is implementation ofjava.net.HttpURLConnection
interface.Try extending
sun.net.www.protocol.HttpURLConnection
and other appropriate classes likeNetworkClient
andHttpClient
and protocol handler classes fromsun.www.
packages.Well i dont have that particular answer for the same but ya u can try this may be i am not sure that will work or not but try once.
Use linux command "ip addr" which gives ouput similar to:
[root@user ~]# ip addr
By default linux uses first ip address as source ip adress on perticular adapter. For example, for above system configuration linux will use "100.10.52.15/24" as source ip adress.
You can write java program with "ProcessBuilder" to execute following commands change order of ip address. (commands: ip addr del 100.10.25.15/24 dev eth0
ip addr add 100.10.25.15/24 dev eth0)
Delete all ip addresses on perticular adapter, store it in memory. Add it in order you want (IP address which should use as source ip should add first)
For example ip adress "100.10.52.16/24" should use as source ip adress then