2013-02-16 64 views
2

Jsoup給出超時錯誤。我怎樣才能解決這個問題?jsoup SocketTimeout異常

的代碼,讓錯誤的線路

Document doc; 
doc = Jsoup.connect("http://google.com").timeout(300000).get(); 

例外,我得到的是

java.net.SocketTimeoutException: Read timed out 
    at java.net.SocketInputStream.socketRead0(Native Method) 
    at java.net.SocketInputStream.read(SocketInputStream.java:150) 
    at java.net.SocketInputStream.read(SocketInputStream.java:121) 
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235) 
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:275) 
    at java.io.BufferedInputStream.read(BufferedInputStream.java:334) 
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:633) 
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:579) 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1322) 
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468) 
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:412) 
    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) 
+2

你能訪問google.com從你的瀏覽器?你是否支持代理? – Neal 2013-02-16 16:08:10

+0

有解決方案,但它們都不令人滿意 – 2013-02-17 02:39:42

回答

3

我認爲你是落後的代表。如果您知道代理詳細信息,則可以嘗試以下操作:

System.setProperty("http.proxyHost", "147.167.10.2");//replace with your proxy host 
System.setProperty("http.proxyPort", "8080");//replace with your proxy port 
Document doc = Jsoup.connect("http://google.com").get(); 
0

我遇到了與jsoup 1.7.3相同的問題。

Jsoup似乎有一個bug,通過下面的代碼證實(從我的應用程序拷貝,但未單元測試,所以也許這個錯誤並不總是重複性):

Document doc; 
URLConnection con = new URL("http://google.com").openConnection(); 
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream())); 
String html; 
while ((html = reader.readLine()) != null) { 
    System.out.println(url + ": "+ html); 
} 
doc = Jsoup.connect("http://google.com").timeout(300000).get();