Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the remote ip address with httpurlconnection

Tags:

java

http

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());
like image 426
Pablojim Avatar asked Jan 23 '23 17:01

Pablojim


2 Answers

URL url = new URL("http://yahoo.com");
String host = url.getHost();
InetAddress address = InetAddress.getByName(host);
String ip = address.getHostAddress();
like image 194
Bozho Avatar answered Jan 25 '23 08:01

Bozho


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.

like image 22
jarnbjo Avatar answered Jan 25 '23 08:01

jarnbjo