简体   繁体   English

如何从Java中的域名获取IP地址?

[英]How to get the IP address from the Domain Name in Java?

I am writing an application where I need the IP address.我正在编写一个需要 IP 地址的应用程序。 I have a domain name and I would like to know how to get the IP address from it.我有一个域名,我想知道如何从中获取 IP 地址。 For example, "www.girionjava.com".例如,“www.girionjava.com”。 How could I get the IP address of this website by programming in Java?怎么在Java编程得到这个网站的IP地址? Thanks.谢谢。

InetAddress giriAddress = java.net.InetAddress.getByName("www.girionjava.com");

然后,如果您想将IP作为字符串

String address = giriAddress.getHostAddress();

This should be simple. 这应该很简单。

InetAddress[] machines = InetAddress.getAllByName("yahoo.com");
for(InetAddress address : machines){
  System.out.println(address.getHostAddress());
}
InetAddress.getByName("www.girionjava.com")

(Extra mask in printing sine java considers all integers to be signed, but an IP address is unsigned) (打印正弦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();
}

The .netAddress class in java.net has some static methods to do this. java.net 中的 .netAddress class 有一些 static 方法可以做到这一点。

        InetAddress a = InetAddress.getByName ("www.girionjava.com");
        System.out.println(a.getHostAddress());     

This is very basic stuff, for more complete control over DNS queries, you will have to use additional libraries.这是非常基本的东西,要更完整地控制 DNS 查询,您将不得不使用其他库。

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

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