URL equal and Internet access check

The http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html says that:

Compares this URL for equality with another object.

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

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

Two hosts are considered equivalent if both host names can be resolved to the same IP addresses; otherwise, if the host name cannot be resolved, host names should be equal regardless of the case; or both host names are zero.

Since host comparison requires the name resolution, this operation is a lock.

Note. Certain peer-to-peer behavior is known to be incompatible with shared hosting in HTTP.

Accordingly, equals will only work if name resolution is possible. Since I cannot be sure that the computer has Internet access at a given time, should I just use strings to store addresses? Also, how do I get tested if access is available upon request?

+3
source share
3 answers

You must use the java.net.URI class. It is equal to a method that works correctly.

As for connection testing, I would say that you need to try to open the socket connection to the given address and port.

+6
source

URL- . URL.equals() , . (~ 40x). , , .

(, , /)

+3

[java.net.URI][1] URL, java.net.URL. URI.

URI, URI. , , :

  • <scheme> <domain>
  • escape-
  • <path>
  • URL <fragment> <user-info>.

Then the problem arises that different URLs can resolve to the same resource using permanent or temporary redirection or simply by returning identical content. And one URL can be resolved for different resources, depending on the headers of HTTP requests, source IP address, moon phase, etc.

+2
source

Source: https://habr.com/ru/post/1746846/


All Articles