How do I get the remote ip address with httpurlconnection
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(SUCC开发者_JS百科ESS, "made connection to: " + urlConn.getRemoteIp());
URL url = new URL("http://yahoo.com");
String host = url.getHost();
InetAddress address = InetAddress.getByName(host);
String ip = address.getHostAddress();
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.
精彩评论