使用java读取URL时读取超时错误

时间:2014-02-10 09:35:02

标签: java url

我已经使用jsoup-1.7.3.jar返回一个代码来读取一个网页,它在一些网站上工作但是为一些URls提供了读取超时错误....

  

线程中的异常“main”java.net.SocketTimeoutException:读取时间   out at java.net.SocketInputStream.socketRead0(Native Method)at   java.net.SocketInputStream.read(SocketInputStream.java:152)at   java.net.SocketInputStream.read(SocketInputStream.java:122)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:687)at   sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)at   sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1323)     在   java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)     在   org.jsoup.helper.HttpConnection $ Response.execute(HttpConnection.java:443)     在   org.jsoup.helper.HttpConnection $ Response.execute(HttpConnection.java:424)     在org.jsoup.helper.HttpConnection.execute(HttpConnection.java:178)     在org.jsoup.helper.HttpConnection.get(HttpConnection.java:167)at   Main.main(Main.java:10)

2 个答案:

答案 0 :(得分:3)

如上所述ooxi,您可以设置超时

Jsoup.connect("").timeout(5*1000).get() //which sets timeout for 5 seconds

编辑:您可以通过连接指定超时

Connection connection = Jsoup.connect("");
connection.timeout(5*1000); // which sets timeout for 5 seconds

答案 1 :(得分:2)

在致电.get之前,您可以设置超时,例如

Jsoup.connect(url).timeout(0).get();

查看JsoupConnection

的JavaDoc