jsoup-Hint Java.net.SocketTimeoutException:Read timed out

Source: Internet
Author: User

Use Jsoup.connect (URL). Get () occasionally appears when you connect to a Web site

Java.net.SocketTimeoutException:Read timed out exception.

The reason is that the default socket latency is relatively short, and some websites respond slowly, so a timeout occurs.

Workaround:

When linking, set the time-out period.

doc = jsoup.connect (URL). Timeout (.). get ();

5000 indicates that the delay time is set to 5s.

The test code is as follows:

1, when timeout is not set:

 Packagejsouptest; Importjava.io.IOException; Importorg.jsoup.*; Importorg.jsoup.helper.Validate; Importorg.jsoup.nodes.Document; Importorg.jsoup.nodes.Element; Importorg.jsoup.select.Elements;  Public classJsouptest { Public Static  voidMain (string[] args)throwsioexception{String URL= "Http://www.weather.com.cn/weather/101010400.shtml"; LongStart =System.currenttimemillis (); Document Doc=NULL; Try{doc=jsoup.connect (URL). get (); }      Catch(Exception e) {e.printstacktrace (); }      finally{System.out.println ("Time is:" + (System.currenttimemillis ()-start) + "MS"); } Elements Elem= Doc.getelementsbytag ("Title"); System.out.println ("Title is:" +Elem.text ()); }     }  

Sometimes timeouts occur:

Java.net.SocketTimeoutException:Read timed out

2, set the general will not time out

 Packagejsouptest; Importjava.io.IOException; Importorg.jsoup.*; Importorg.jsoup.helper.Validate; Importorg.jsoup.nodes.Document; Importorg.jsoup.nodes.Element; Importorg.jsoup.select.Elements;  Public classJsouptest { Public Static  voidMain (string[] args)throwsioexception{String URL= "Http://www.weather.com.cn/weather/101010400.shtml"; LongStart =System.currenttimemillis (); Document Doc=NULL; Try{doc= Jsoup.connect (URL). Timeout (5000). get (); }      Catch(Exception e) {e.printstacktrace (); }      finally{System.out.println ("Time is:" + (System.currenttimemillis ()-start) + "MS"); } Elements Elem= Doc.getelementsbytag ("Title"); System.out.println ("Title is:" +Elem.text ()); }     }  

jsoup-Hint Java.net.SocketTimeoutException:Read timed out

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.