我正在编写一个应用程序,需要获取IP地址。我有一个域名,想知道如何从中获取IP地址。例如,“www.girionjava.com”。我该如何在Java编程中获取此网站的IP地址?谢谢。
我正在编写一个应用程序,需要获取IP地址。我有一个域名,想知道如何从中获取IP地址。例如,“www.girionjava.com”。我该如何在Java编程中获取此网站的IP地址?谢谢。
InetAddress giriAddress = java.net.InetAddress.getByName("www.girionjava.com");
接下来,如果你想要将IP地址转换为字符串
String address = giriAddress.getHostAddress();
这应该很简单。
InetAddress[] machines = InetAddress.getAllByName("yahoo.com");
for(InetAddress address : machines){
System.out.println(address.getHostAddress());
}
InetAddress.getByName("www.girionjava.com")
(在打印正弦Java时,额外的掩码考虑所有整数都是有符号的,但IP地址是无符号的)
InetAddress[] machines = InetAddress.getAllByName("yahoo.com");
for(InetAddress address : machines){
byte[] ip = address.getAddress();
for(byte b : ip){
System.out.print(Integer.toString(((int)b)&0xFF)+".");
}
System.out.println();
}
InetAddress a = InetAddress.getByName ("www.girionjava.com");
System.out.println(a.getHostAddress());
这是非常基础的东西,如果你想要更完整地控制 DNS 查询,你就需要使用额外的库。