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?
From stackoverflow
-
Don't use URL.equals. As the documentation says,
Note: The defined behavior for
equalsis known to be inconsistent with virtual hosting in HTTP.Mnementh : How should I test for equality?Chris Jester-Young : 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. -
If you just want to compare the url strings, try
url1.toString().equals(url2.toString())Bill the Lizard : The two strings could be vastly different but point to the same resource.Rick : 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". -
Use URI instead of URL.
Gareth : And of course this makes semantic sense too, because you want to compare the Identifiers rather than the Locations
0 comments:
Post a Comment