如何检查 URL 是否不存在?

发布于 2024-12-11 12:27:04 字数 474 浏览 0 评论 0原文

我试图检查我想要连接的 URL 是否存在。这是我的尝试:

try {
    // Connect to the url
    document = Jsoup.connect("http://www.malformedurl.com").get();
    tags = document.select(".tags .tag a");
    num = document.select(".tag .count");
    // Take the wanted data 
    UrlFunctions.UrlParse(tags, num);
} catch (java.net.MalformedURLException e) {
    System.out.println("URL DOESNT EXIST");
}

运行后,我没有收到消息 URL DOESNT EXIST。我应该使用什么例外或者我还应该做什么?

I trying to check if an URL which I want to connect to exists or not. Here's my attempt:

try {
    // Connect to the url
    document = Jsoup.connect("http://www.malformedurl.com").get();
    tags = document.select(".tags .tag a");
    num = document.select(".tag .count");
    // Take the wanted data 
    UrlFunctions.UrlParse(tags, num);
} catch (java.net.MalformedURLException e) {
    System.out.println("URL DOESNT EXIST");
}

After running that, I don't get the message URL DOESNT EXIST. What exception should I use or what else should I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风为裳 2024-12-18 12:27:04

MalFormedURLException 仅当 URL 确实格式错误时才会抛出,即不符合 URL 规范,当它不存在时则不。这是由 java.net.URL 类的构造函数抛出的。它的 javadoc 讲述以下内容:

抛出

MalformedURLException - 如果字符串指定未知协议。

因此,只有当您使用例如 "www.malformedurl.com"
“foo://www.malformedurl.com”而不是“http://www.malformedurl.com”

要检测 URL 是否存在,您最好采用不同的解决方案。如果主机名未知,那么您应该捕获 < code>UnknownHostException 相反:

try {
    document = Jsoup.connect("http://www.malformedurl.com").get();
    // ...
} catch (UnknownHostException e) {
    System.err.println("Unknown host");
    e.printStackTrace(); // I'd rather (re)throw it though.
}

这不一定是另一端的问题,当您网络上的 DNS 服务器是假的时也可能发生。

或者,要检测 IP 地址是否可达,您应该捕获 SocketTimeoutException 相反:

try {
    document = Jsoup.connect("http://12.34.56.78").get();
    // ...
} catch (SocketTimeoutException e) {
    System.err.println("IP cannot be reached");
    e.printStackTrace(); // I'd rather (re)throw it though.
}

A MalFormedURLException will only be thrown when the URL is really malformed, i.e. it does not conform the URL spec, not when it does not exist. This is under the covers been thrown by the constructor of the java.net.URL class. Its javadoc tells the following:

throws

MalformedURLException - If the string specifies an unknown protocol.

So, it will only be thrown when you use for example "www.malformedurl.com" or
"foo://www.malformedurl.com" instead of "http://www.malformedurl.com".

To detect whether an URL exists you'd better to head for a different solution. If the host name is unknown, then you should catch UnknownHostException instead:

try {
    document = Jsoup.connect("http://www.malformedurl.com").get();
    // ...
} catch (UnknownHostException e) {
    System.err.println("Unknown host");
    e.printStackTrace(); // I'd rather (re)throw it though.
}

This is not necessarily a problem of the other end, it can also occur when the DNS server on your network is bogus.

Or, to detect whether an IP address is reachable, then you should catch SocketTimeoutException instead:

try {
    document = Jsoup.connect("http://12.34.56.78").get();
    // ...
} catch (SocketTimeoutException e) {
    System.err.println("IP cannot be reached");
    e.printStackTrace(); // I'd rather (re)throw it though.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文