tags:

views:

279

answers:

3

The equals()-method of the URL-class in the Java-class-library makes a DNS-request to get the IP for the hostname, to check the two IP's for equality. This happens even for URL's, that are created from the same String. Is there a way to avoid this internet-access?

A: 

Don't use URL.equals. As the documentation says,

Note: The defined behavior for equals is known to be inconsistent with virtual hosting in HTTP.

Chris Jester-Young
How should I test for equality?
Mnementh
You can use toString() or toExternalForm() to get the external form, and compare those. Preliminary testing here shows it doesn't access DNS.
Chris Jester-Young
I just upvoted Bill's answer. Just call toURI() on your URL objects.
Chris Jester-Young
+2  A: 

If you just want to compare the url strings, try

url1.toString().equals(url2.toString())
Rick
The two strings could be vastly different but point to the same resource.
Bill the Lizard
If you want to check the resource referred to by a URL, you obviously need internet access. Therefore I made the assumption that he wanted to compare the urls themselves. Hence my qualification of "if you just want to compare the url strings".
Rick
+11  A: 

Use URI instead of URL.

Bill the Lizard
And of course this makes semantic sense too, because you want to compare the Identifiers rather than the Locations
Gareth