tags:

views:

80

answers:

2

I'm connecting to a url with a httpurlconnection in java 1.6

The server I connect uses DNS round robin to share load between multiple servers.

How can I get the remote ip address that I have actually connected to?

HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
//I then need something like this!
log(SUCCESS, "made connection to: " + urlConn.getRemoteIp());
+1  A: 
URL url = new URL("http://yahoo.com");
String host = url.getHost();
InetAddress address = InetAddress.getByName(host);
String ip = address.getHostAddress();
Bozho
And this might give the right IP since the jvm caches DNS queries, unless that is disabled on the jvm
nos
+2  A: 

Not directly, but since the JVM is caching DNS lookups, you can use InetAddress.getByName(serverName) to find the actual IP address being used unless you've set the system property "networkaddress.cache.ttl" to disable the cache.

jarnbjo