如何检查 URL 是否不存在?
我试图检查我想要连接的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MalFormedURLException
仅当 URL 确实格式错误时才会抛出,即不符合 URL 规范,当它不存在时则不。这是由java.net.URL
类的构造函数抛出的。它的 javadoc 讲述以下内容:因此,只有当您使用例如
"www.malformedurl.com"
或“foo://www.malformedurl.com”
而不是“http://www.malformedurl.com”
。要检测 URL 是否存在,您最好采用不同的解决方案。如果主机名未知,那么您应该捕获 < code>UnknownHostException 相反:
这不一定是另一端的问题,当您网络上的 DNS 服务器是假的时也可能发生。
或者,要检测 IP 地址是否可达,您应该捕获
SocketTimeoutException
相反: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 thejava.net.URL
class. Its javadoc tells the following: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: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: