Throw new exceptions for the pretending URL

I did something that retrieves the IP address of the URL that I entered.

InetAddress ip = InetAddress.getByName("www.fake.cao");
return ia.getHostAddress();

I also threw an UnknownHostException to try to catch an error for invalid urls.

The problem is that www.fake.cao is recognized as a valid URL and returns an inaccessible IP address to me and does not throw an exception.

Can someone tell me what I can do to catch these pretentious URLs?

+5
source share
3 answers

The problem is that your ISP is returning a fake page for a non-existent DNS domain. This (bad) practice is known as NXDOMAIN redirection.

JRE , 81.200.64.50 IP- www.fake.cao, , -.

, ( , , , , HTTP-) IP- IP- (, does-not-exist.invalid).

InetAddress ip = InetAddress.getByName("www.fake.cao");

InetAddress fake;
try {
   fake = InetAddress.getByName("does-not-exist.invalid");      
} catch (UnknownHostException e) {
   //www.fake.cao exists, but invalid does not
   return ip;
} 
if (Arrays.equals(ip.getAddress(),fake.getAddress())) {
    //both ip and fake resolves to the same IP
    throw new UnknownHostException();
}

//invalid does exist, but it is different from ip
return ip;

, - , .

+1

, , .

+2

IP 0.0.0.0 , , . URL-, , IP-. 0.0.0.0, , , .

If you get some kind of common non-existent address from your ISP, you might try to search for NS NS manually. Basically, you will have to rewrite all the code in the InetAddress class. This will include obtaining the IP address of the DNS server, opening the socket for it, publishing the NS request, and reading the parsing of the response.

If you are not using platform independence, you can use JNI to connect to the platform’s DNS tool. nslookup.exefor windows for example.

+1
source

All Articles