程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java應用Jsoup銜接網站超時的處理辦法

java應用Jsoup銜接網站超時的處理辦法

編輯:關於JAVA

java應用Jsoup銜接網站超時的處理辦法。本站提示廣大學習愛好者:(java應用Jsoup銜接網站超時的處理辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是java應用Jsoup銜接網站超時的處理辦法正文


明天做了一個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());
 } 
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved