如何得到网站的IP地址?
需要得到类似www.sohu.com这样的网站的IP地址,我写了如下代码:
InetAddress id=InetAddress.getByName( "www.sohu.com ");
byte[] ip=id.getAddress();
int i;
for (i=0;i <ip.length;i++) {
if (i> 0) System.out.print( ". ");
System.out.print(ip[i]& 0xff);
}
总是无法找到,因为我们是通过代理上网的,所以也设了代理:
Properties prop = System.getProperties(); prop.setProperty( "http.proxyHost ", " ");
prop.setProperty( "http.proxyPort ", " ");
没有用,还是得不到!
------解决方案--------------------我试了,可以
package qujianfeng;
import java.net.InetAddress;
import java.net.
UnknownHostException;
public class GetIP {
public static String getIP(String url) throws UnknownHostException{
InetAddress id=InetAddress.getByName(url);
return id.getHostAddress();
}
public static void main(String[] args) throws UnknownHostException {
System.out.println(getIP( "www.sohu.com "));
}
}