ECM
ECM
总版主
总版主
  • 社区居民
  • 忠实会员
  • 原创写手
阅读:678回复:0

java使用Jsoup连接网站超时的解决方法

楼主#
更多 发布于:2015-12-31 16:37
今天做了一个Jsoup解析网站的项目,使用Jsoup.connect(url).get()连接某网站时偶尔会出现
java.net.SocketTimeoutException:Read timed out异常。
原因是默认的Socket的延时比较短,而有些网站的响应速度比较慢,
所以会发生超时的情况。
解决方法:


链接的时候设定超时时间即可。
doc = Jsoup.connect(url).timeout(5000).get();
5000表示延时时间设置为5s。
测试代码如下:
1,不设定timeout时:
复制代码 代码如下:


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());
 }
}
有时发生超时:
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 设定了则一般不会超时
复制代码 代码如下:


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

 

 

 

 

 

 

 

异常中心网是一家专门收集整理程序员编程过程中遇到的常见异常(exception)以及各种异常问答中心的网站。异常中心网旨在,减少程序员在编码遇到异常,处理各种异常时间和痛苦,让程序员能更愉快的、快速的定位异常并查找对应的异常解决方案。异常中心网诚心打造最完美的编程社区为程序员用户服务,努力成为最好的程序员乐园程序员社区程序异常中心程序bug中心异常问答中心

 

喜欢0 评分0
游客

返回顶部