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.
Right. So that’s the IP address you got for
www.tesco.com.Because that server (an Akamai accelerator) has no idea which web site you want.
Right, because
tesco.comis not the same aswww.tesco.com.Because that web server did know which web site you wanted because it only handles
tesco.com, unlike the servers that handlewww.tesco.comthat handle many web sites.If you look closely, you’ll see that pointing your browser at
tesco.comredirects you towww.tesco.com. They use Akamai.