Java use Jsoup to connect Web site timeout Solution _java

Source: Internet
Author: User

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 ());
}
}

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.