URL equals and checking Internet access

On http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html it states that:

Compares this URL for equality with another object.

If the given object is not a URL then
this method immediately returns false.

Two URL objects are equal if they have
the same protocol, reference
equivalent hosts, have the same port
number on the host, and the same file
and fragment of the file.

Two hosts are considered equivalent if
both host names can be resolved into
the same IP addresses; else if either
host name can’t be resolved, the host
names must be equal without regard to
case; or both host names equal to

Since hosts comparison requires name
resolution, this operation is a
blocking operation.

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

According to this, equals will only work if name resolution is possible. Since I can’t be sure that a computer has internet access at a given time, should I just use Strings to store addresses instead? Also, how do I go about testing if access is available when requested?


You should use the java.net.URI class. It’s equals method works properly.

As for testing connection, I’d say you need to try to open socket connection on given address and port.

Leave a Reply

Your email address will not be published. Required fields are marked *