gpt4 book ai didi

java - 在 Jsoup 中使用 execute() 时出现 SocketTimeoutException

转载 作者:太空宇宙 更新时间:2023-11-04 13:49:01 24 4
gpt4 key购买 nike

当我尝试使用 Jsoup 解析 HTML 页面时,出现 SocketTimeoutException:

java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
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.HttpClient.parseHTTPHeader(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:381)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:364)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:143)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:132)
at app.ForumCrawler.crawl(ForumCrawler.java:50)
at Main.main(Main.java:15)

我使用了这部分代码来解析页面,因为我寻找像 200,404 等响应。

String userAgent = "Mozilla/5.0 (jsoup)";
int timeout = 5 * 1000;
Document localDoc = null;
String url = "<url>";
Connection.Response response = Jsoup.connect(url).userAgent(userAgent).timeout(timeout).execute();
if(response.statusCode() == 200) {
localDoc = Jsoup.parse(response.body());
//do the stuff..
}

我发现,如果我们使用.get()而不是.execute(),我们可以摆脱SocketTimeoutException问题,但如果我使用.get(),那么我无法获得响应。

请建议我使用哪一个来摆脱 SocketTimeoutException 并在尝试解析页面时获得响应

提前致谢。

最佳答案

I have come across that if we use .get() instead of .execute() we can get rid of SocketTimeoutException issue

正如您在帖子中的调用堆栈中看到的,get() 方法在内部调用 execute() 方法:

at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:381)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:364)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:143)<-- execute() called
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:132)<-- ...from get().
at app.ForumCrawler.crawl(ForumCrawler.java:50)

所以你无法摆脱SocketTimeoutException。但是,您可以通过处理此异常来增强代码。

关于java - 在 Jsoup 中使用 execute() 时出现 SocketTimeoutException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30522377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com