Mnementh
Mnementh

Reputation: 51331

How to avoid, that URL.equals needs access to the internet in Java?

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?

Upvotes: 13

Views: 3111

Answers (3)

Bill the Lizard
Bill the Lizard

Reputation: 406015

Use java.net.URI instead of URL.

Upvotes: 23

Rick
Rick

Reputation: 4763

If you just want to compare the url strings, try

url1.toString().equals(url2.toString())

Upvotes: 4

C. K. Young
C. K. Young

Reputation: 223173

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.

Upvotes: 2

Related Questions