简体   繁体   English

jsoup检查链接是否存在

[英]jsoup to check if a link exsists

I want to check if a link exists, if does get the homepage. 我想检查链接是否存在,是否获取主页。

Document document = Jsoup.connect(link).get();

The issue with this is some times I get : 我有时遇到的问题是:

Exception in thread "main" java.nio.charset.UnsupportedCharsetException: X-MAC-ROMAN
    at java.nio.charset.Charset.forName(Unknown Source)
    at org.jsoup.helper.DataUtil.parseByteData(DataUtil.java:86)
    at org.jsoup.helper.HttpConnection$Response.parse(HttpConnection.java:469)
    at org.jsoup.helper.HttpConnection.get(HttpConnection.java:147)

I got the related question answered by balu that: 我得到了balu回答的相关问题:

Document doc = Jsoup.parse(new URL(link).openStream(), "ISO-8859-1", link);

should be used when the char encoding is different. 当char编码不同时应使用。

But how can I get this working in a case I want to do page validation. 但是,在我要进行页面验证的情况下,如何使它正常工作。

I will get link from user: 我会得到来自用户的链接:

than I try 比我尝试的

Document document = Jsoup.connect(link).get();

It gives exception when the char encoding is not default. 当char编码不是默认值时,它将给出异常。

Why you dont use Jsoup.parse(...) instead? 为什么不使用Jsoup.parse(...)代替呢? This will throw an UnknownHostException if your link doesnt exist, else it will return your Document . 如果您的链接不存在,则会抛出UnknownHostException ,否则将返回Document

However you can use connect() in a try/catch block, if an UnsupportedCharsetException is thrown you can call the parse() -method in the catch block. 但是,您可以在try / catch块中使用connect() ,如果抛出UnsupportedCharsetException则可以在catch块中调用parse() -方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM