gpt4 book ai didi

java - URL 等于并检查 Internet 访问

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:43:32 26 4
gpt4 key购买 nike

关于 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com