简体   繁体   中英

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(SUCCESS, "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();

不是直接的,但由于JVM正在缓存DNS查找,您可以使用InetAddress.getByName(serverName)来查找正在使用的实际IP地址,除非您已将系统属性“networkaddress.cache.ttl”设置为禁用缓存。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM