0

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.

Neethu Lalitha
  • 3,031
  • 4
  • 35
  • 60
  • Does this answer your question? [Need to perform a reverse DNS lookup of a particular IP address in java](https://stackoverflow.com/questions/7097623/need-to-perform-a-reverse-dns-lookup-of-a-particular-ip-address-in-java) – maio290 Jan 28 '21 at 22:01
  • getCanonicalHostName() didnt help. It gives "test.com" not thisistest.com – Neethu Lalitha Jan 28 '21 at 22:04
  • Learn about DNS and name resolution. You can not always directly go back and forth between IP and DNS name. One example would be if you setup one server (with one IP) and have several DNS entries pointing to it. – Queeg Jan 31 '21 at 07:45

0 Answers0