How can I get the hostname from the url using Java. I tried below:
InetAddress.getByName(new URL(url).getHostName());
This works only if the hostnames are the same as below. Here both are stackoverflow.com.But some times you get a different hostname :
Example when I use a linux or windows box to ping a host:
ping **stackoverflow.com**
PING **stackoverflow.com** (151.101.193.69): 56 data bytes
64 bytes from 151.101.193.69: icmp_seq=0 ttl=58 time=37.932 ms
64 bytes from 151.101.193.69: icmp_seq=1 ttl=58 time=36.935 ms
64 bytes from 151.101.193.69: icmp_seq=2 ttl=58 time=36.472 ms
64 bytes from 151.101.193.69: icmp_seq=3 ttl=58 time=38.342 ms
ping **test.com**
PING **thisistest.com** (151.101.193.69): 56 data bytes
64 bytes from 151.101.193.69: icmp_seq=0 ttl=58 time=37.932 ms
64 bytes from 151.101.193.69: icmp_seq=1 ttl=58 time=36.935 ms
64 bytes from 151.101.193.69: icmp_seq=2 ttl=58 time=36.472 ms
64 bytes from 151.101.193.69: icmp_seq=3 ttl=58 time=38.342 ms
If you see for test.com
when pinged I get thisistest.com
. How to get thisistest.com name using Java.