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