The return of .getInetAddress() is not the actual IP
-
18-06-2021 - |
문제
I have the following piece of code from a function that takes the host name as input and supposed to extract the certificate for that host. I need to display the IP address for the remote host. I used ".getInetAdress()" to return the IP for the remote host: 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();
When I run the program, for example, the host is: "www.tesco.com", the ".getInetAddress()." returns this IP to me: 88.221.94.232. When I try to type this IP in the browser, it gives me "Invalid URL", even after adding 'https://' to the URL. While, if I tried to ping "tesco.com" I get another IP: "212.140.185.177" and if I typed it in the browser, it opens tesco's web page.
What am I misunderstanding ? Is there any other methods other than getInetAddress & getHostAddress() to get the IP address for a remote host (without using socket)?
해결책
You are getting the correct IP address.
When I run the program, for example, the host is: "www.tesco.com", the ".getInetAddress()." returns this IP to me: 88.221.94.232.
Right. So that's the IP address you got for www.tesco.com
.
When I try to type this IP in the browser, it gives me "Invalid URL".
Because that server (an Akamai accelerator) has no idea which web site you want.
While, if I tried to ping "tesco.com" I get another IP: "212.140.185.177"
Right, because tesco.com
is not the same as www.tesco.com
.
and if I typed it in the browser, it opens tesco's web page.
Because that web server did know which web site you wanted because it only handles tesco.com
, unlike the servers that handle www.tesco.com
that handle many web sites.
If you look closely, you'll see that pointing your browser at tesco.com
redirects you to www.tesco.com
. They use Akamai.