在我的程序中,用户输入url字符串,例如
http://www.engineering.uiowa.edu/~hawkeng//fall01/graphics/potato.gif
我将如何获得该网址的IP地址?我尝试使用
InetAddress address = InetAddress.getByName(urlStr);
但是结果总是返回null。获取IP地址的正确方法是什么?
最佳答案
尝试这个:
InetAddress address = InetAddress.getByName(new URL(urlString).getHost());
要获取原始IP:
String ip = address.getHostAddress();