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

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

  •  
  •   sandman511 · 6 天前 · 357 次点击

    问题 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   6 天前
    jsoup 的 connectTimeout 是传入的 30s,而 readTimeout 是 30 / 2 = 15s,这里应该是握手完成了然后收不到数据包超时吧,所以是 15s + 握手延迟。
    我这边测下来一直在 16s ~ 22s 徘徊。
    关于   ·   FAQ   ·   API   ·   我们的愿景   ·   广告投放   ·   感谢   ·   实用小工具   ·   2261 人在线   最高记录 5168   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.3 · 31ms · UTC 13:50 · PVG 21:50 · LAX 05:50 · JFK 08:50
    ♥ Do have faith in what you're doing.