繁体   English   中英

如何使用httpurlconnection获取远程IP地址

[英]How do I get the remote ip address with httpurlconnection

我在java 1.6中连接到一个带有httpurlconnection的url

我连接的服务器使用DNS循环来共享多个服务器之间的负载。

如何获取我实际连接的远程IP地址?

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”设置为禁用缓存。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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