我从一个函数中获得了以下代码,该函数将主机名作为输入并应该提取该主机的证书。我需要显示远程主机的 IP 地址。我使用“.getInetAdress()”返回远程主机的 IP:http://docs.oracle.com/javase/1.5.0/docs/api/java/net/Socket.html#getInetAddress( )。
SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory();
System.out.println("Starting SSL Socket For "+hostname+" port "+port);
SSLSocket socket = (SSLSocket) factory.createSocket(hostname, port);
//To get the IP for the remote host. Format: (domain name/IP),
//then manually covert it to string
String remoteIP=socket.getInetAddress().toString();
System.out.println("Remote address = " + remoteIP);
socket.startHandshake();
Certificate[] serverCerts = socket.getSession().getPeerCertificates();
例如,当我运行程序时,主机是:“www.tesco.com”,即“.getInetAddress()”。将此 IP 返回给我:88.221.94.232。当我尝试在浏览器中键入此 IP 时,它给了我“无效的 URL”,即使在 URL 中添加了“https://”之后也是如此。同时,如果我尝试 ping “tesco.com”,我会得到另一个 IP:“212.140.185.177”,如果我在浏览器中输入它,它会打开 tesco 的网页。
我有什么误解?除了 getInetAddress 和 getHostAddress() 之外,还有其他方法可以获取远程主机的 IP 地址(不使用套接字)吗?