Java JSOUP 连接超时问题及乱码问题求助

2020-01-15 09:57:46 +08:00
 sandman511

问题 1:已经设置了超时时间为 30 秒 且 JSOUP 默认超时时间为 30S,为什么输出的超时时间是 20 秒
问题 2:IDEA 已经加了 JVM 参数 && gradle 也加了 UTF-8 的编译方式,为什么还是会有乱码?且并不是全乱码,仅仅最后一个中文字符乱码,感觉跟句末的空格有联系?(同样的配置方法 本来用的 IDEA 正式版 刚刚换了社区版 正式版我 gradle 都没设定编译方式都可以正常输出)

        long t1 = System.currentTimeMillis();
        Document document = null;
        try {
            document = Jsoup.connect(URL)
                    .cookies(COOKIES).timeout(30000).post();
        } catch (IOException e) {
            long t2 = System.currentTimeMillis() - t1;
            System.out.println("连接失败,连接时间 " + t2 + " ms");
            e.printStackTrace();
        }

输出

连接失败,连接时�? 19085 ms
java.net.SocketTimeoutException: Read timed out
	at java.net.SocketInputStream.socketRead0(Native Method)
	at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
	at java.net.SocketInputStream.read(SocketInputStream.java:171)
	at java.net.SocketInputStream.read(SocketInputStream.java:141)
	at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
	at sun.security.ssl.InputRecord.read(InputRecord.java:503)
	at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:975)
	at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:933)
	at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
	at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
	at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
	at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
	at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:735)
	at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1593)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498)
	at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:352)
	at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:734)
	at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:705)
	at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:295)
	at org.jsoup.helper.HttpConnection.post(HttpConnection.java:290)
	at main.monitor(main.java:87)
	at main.lambda$main$0(main.java:40)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
1356 次点击
所在节点    程序员
1 条回复
fzhyzamt
2020-01-15 11:47:12 +08:00
jsoup 的 connectTimeout 是传入的 30s,而 readTimeout 是 30 / 2 = 15s,这里应该是握手完成了然后收不到数据包超时吧,所以是 15s + 握手延迟。
我这边测下来一直在 16s ~ 22s 徘徊。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/638009

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX