How to avoid, that URL.equals needs access to the internet in Java?
Question
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 URLs that are created from the same String
. Is there a way to avoid this internet access?
Solution
Use java.net.URI instead of URL.
OTHER TIPS
If you just want to compare the url strings, try
url1.toString().equals(url2.toString())
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow