Today I did a jsoup to resolve the Web site, using the Jsoup.connect (URL). Get () occasionally occurs when you connect to a Web site
Java.net.SocketTimeoutException:Read timed out exception.
The reason is that the default socket delay is relatively short, and some Web sites are slower response speed,
So the timeout situation occurs.
Workaround :
Set the timeout time when linking.
doc = jsoup.connect (URL). Timeout (5000). get ();
5000 indicates that the delay time is set to 5s.
The test code is as follows:
1, do not set timeout:
Copy Code code as follows:
Package jsouptest;
Import java.io.IOException;
Import org.jsoup.*;
Import Org.jsoup.helper.Validate;
Import org.jsoup.nodes.Document;
Import org.jsoup.nodes.Element;
Import org.jsoup.select.Elements;
public class Jsouptest {
public static void Main (string[] args) throws ioexception{
String url = "Http://www.jb51.net";
Long start = 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
At Java.net.SocketInputStream.socketRead0 (Native method)
At Java.net.SocketInputStream.read (Unknown Source)
At Java.net.SocketInputStream.read (Unknown Source)
At Java.io.BufferedInputStream.fill (Unknown Source)
At Java.io.BufferedInputStream.read1 (Unknown Source)
At Java.io.BufferedInputStream.read (Unknown Source)
At Sun.net.www.http.ChunkedInputStream.fastRead (Unknown Source)
At Sun.net.www.http.ChunkedInputStream.read (Unknown Source)
At Java.io.FilterInputStream.read (Unknown Source)
At Sun.net.www.protocol.http.httpurlconnection$httpinputstream.read (Unknown Source)
At Java.util.zip.InflaterInputStream.fill (Unknown Source)
At Java.util.zip.InflaterInputStream.read (Unknown Source)
At Java.util.zip.GZIPInputStream.read (Unknown Source)
At Java.io.BufferedInputStream.read1 (Unknown Source)
At Java.io.BufferedInputStream.read (Unknown Source)
At Java.io.FilterInputStream.read (Unknown Source)
At Org.jsoup.helper.DataUtil.readToByteBuffer (datautil.java:113)
At Org.jsoup.helper.httpconnection$response.execute (httpconnection.java:447)
At Org.jsoup.helper.httpconnection$response.execute (httpconnection.java:393)
At Org.jsoup.helper.HttpConnection.execute (httpconnection.java:159)
At Org.jsoup.helper.HttpConnection.get (httpconnection.java:148)
At JsoupTest.JsoupTest.main (jsouptest.java:17)
Time is:3885ms
Exception in thread "main" java.lang.NullPointerException
At JsoupTest.JsoupTest.main (jsouptest.java:25)
2 is set, it usually doesn't timeout.
Copy Code code as follows:
Package jsouptest;
Import java.io.IOException;
Import org.jsoup.*;
Import Org.jsoup.helper.Validate;
Import org.jsoup.nodes.Document;
Import org.jsoup.nodes.Element;
Import org.jsoup.select.Elements;
public class Jsouptest {
public static void Main (string[] args) throws ioexception{
String url = "Http://www.jb51.net";
Long start = 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 ());
}
}