简体   繁体   中英

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:

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

should be used when the char encoding is different.

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.

Why you dont use Jsoup.parse(...) instead? This will throw an UnknownHostException if your link doesnt exist, else it will return your 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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