java - URL 等于并检查 Internet 访问

标签 java url

关于 http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html它指出:

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 null.

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.

据此,equals 只有在名称解析可行时才有效。由于我不能确定计算机在给定时间是否可以访问互联网,我是否应该只使用字符串来存储地址?另外,我该如何测试在请求时访问是否可用?

最佳答案

您应该使用 java.net.URI 类。它的 equals 方法可以正常工作。

至于测试连接,我会说你需要尝试在给定的地址和端口上打开套接字连接。

关于java - URL 等于并检查 Internet 访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2896524/

相关文章:

perl - 禁用 HTTP 请求中的 URL 编码

java - 带有 asp.net 的 liferay 门户

java - 无法将 Sea Glass LAF 用于 Java 应用程序

php - 创建动态规范 url

java - 为嵌入式 Jetty 指定 JAR "resources/webapp"文件夹的 ResourceBase 的正确 URL 是什么?

python - 允许/在 django url

php - 从浏览器读取 url

java - Spring Security SAML + HTTPS 到另一个页面

java - 在 java 中添加对 windows 任务栏的贡献 - 类似于笔记本电脑电池小部件

java - HK2 相当于 Guice 中的 @Provides for Jersey 2