V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
sandman511
V2EX  ›  程序员

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

  •  
  •   sandman511 · 2020-01-15 09:57:46 +08:00 · 1356 次点击
    这是一个创建于 1770 天前的主题,其中的信息可能已经有所发展或是发生改变。

    问题 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)
    
    1 条回复    2020-01-15 11:47:12 +08:00
    fzhyzamt
        1
    fzhyzamt  
       2020-01-15 11:47:12 +08:00
    jsoup 的 connectTimeout 是传入的 30s,而 readTimeout 是 30 / 2 = 15s,这里应该是握手完成了然后收不到数据包超时吧,所以是 15s + 握手延迟。
    我这边测下来一直在 16s ~ 22s 徘徊。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2777 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 15:22 · PVG 23:22 · LAX 07:22 · JFK 10:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.