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
2008-11-13 00:53:17
How should I test for equality?
Mnementh
2008-11-13 00:54:24
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
2008-11-13 00:57:15
I just upvoted Bill's answer. Just call toURI() on your URL objects.
Chris Jester-Young
2008-11-13 01:00:08
+2
A:
If you just want to compare the url strings, try
url1.toString().equals(url2.toString())
Rick
2008-11-13 00:58:01
The two strings could be vastly different but point to the same resource.
Bill the Lizard
2008-11-13 01:02:25
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
2008-11-13 01:46:58
And of course this makes semantic sense too, because you want to compare the Identifiers rather than the Locations
Gareth
2008-11-13 01:01:34