How to get the IP address from the Domain Name in

2019-03-17 18:55发布

I am writing an application where I need the IP address. I have a domain name and I would like to know how to get the IP address from it. For example, "www.girionjava.com". How could I get the IP address of this website by programming in Java? Thanks.

4条回答
ゆ 、 Hurt°
2楼-- · 2019-03-17 19:17
InetAddress giriAddress = java.net.InetAddress.getByName("www.girionjava.com");

Then, if you want the IP as a String

String address = giriAddress.getHostAddress();
查看更多
ゆ 、 Hurt°
3楼-- · 2019-03-17 19:21

This should be simple.

InetAddress[] machines = InetAddress.getAllByName("yahoo.com");
for(InetAddress address : machines){
  System.out.println(address.getHostAddress());
}
查看更多
我只想做你的唯一
4楼-- · 2019-03-17 19:21

(Extra mask in printing sine java considers all integers to be signed, but an IP address is unsigned)

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();
}
查看更多
SAY GOODBYE
5楼-- · 2019-03-17 19:22
InetAddress.getByName("www.girionjava.com")
查看更多
登录 后发表回答