JavaでWebサイトのIPアドレスを取得する
このチュートリアルでは、メソッド java.net.InetAddress.getByName() WebサイトのWebサーバーのIPアドレスをJavaで取得するには IPアドレスまたはドメイン名の表現に基づく(例: www.google.com.import java.net.InetAddress;Output:
import java.net.SocketException;
import java.net.UnknownHostException;
public class ServerAddress {
public static void main(String[] zero) throws SocketException {
InetAddress ServerAddress;
try{
ServerAddress= InetAddress.getByName("www.yahoo.fr");
//ドメイン名
System.out.println("Yahoo server address = "+
ServerAddress.getHostName());
//Yahoo サーバーの IP アドレス
System.out.println("Domain name = "+
ServerAddress.getHostName());
System.out.println("サーバのIPアドレス Yahoo.com = "+
ServerAddress.getHostAddress());
System.out.println("サーバーのIPアドレス Yahoo.fr = "+
InetAddress.getByName("www.yahoo.fr").getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
domain name = www.yahoo.comgetHostName(): この IP アドレスのマシン名を返します。
サーバーのIPアドレス Yahoo.com = 46.228.47.115
サーバーのIPアドレス Yahoo.fr = 77.238.184.150
getHostAdress(): サーバーの IPv4 アドレスを返します。
参照:
Doc Oracle: InetAdress
JavaでのIPアドレスの操作