How to get remote IP address using httpurlconnection

I am connecting to url with httpurlconnection in java 1.6

The server I'm connecting to uses DNS round robin to share the load between multiple servers.

How can I get the remote IP address that I really connected to?

HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); //I then need something like this! log(SUCCESS, "made connection to: " + urlConn.getRemoteIp()); 
+4
source share
2 answers
 URL url = new URL("http://yahoo.com"); String host = url.getHost(); InetAddress address = InetAddress.getByName(host); String ip = address.getHostAddress(); 
+4
source

Not directly, but since the JVM caches DNS queries, you can use InetAddress.getByName (server_name) to find the actual IP address that is used if you did not set the system property "networkaddress.cache.ttl" to disable the cache.

+4
source

Source: https://habr.com/ru/post/1298976/


All Articles