gpt4 book ai didi

java urlconnection 不抛出 sockettimeoutexception

转载 作者:搜寻专家 更新时间:2023-11-01 03:55:02 24 4
gpt4 key购买 nike

我一直遇到 java httpurlconnections,即使使用 setReadTimeOut(),也不会在应该抛出 sockettimeoutexception 时抛出异常,并且它始终与最终(挂起 2-3 分钟后)相关联) 过早的 EOF 异常。我在一些博客中读到这可能与使用 BufferedReader 的 readLine 方法进行阅读有关, 字符串 x = ""; while((x=bufferedReader.readLine())!=null){}

如果输入流末尾没有换行符,则在文件末尾。这没有意义,为什么 java 的 setTimeOut 功能不正确?

       URL url=new URL("");
c=(HttpURLConnection)url.openConnection();
c.setReadTimeout(17000);
BufferedReader b = new BufferedReader(new InputStreamReader(c.getInputStream()));
String s;

while((s=b.readLine())!=null)
{
} ect.






java.io.IOException: Premature EOF
at sun.net.www.http.ChunkedInputStream.readAheadBlocking(Unknown Source)
at sun.net.www.http.ChunkedInputStream.readAhead(Unknown Source)
at sun.net.www.http.ChunkedInputStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)

这是响应 header (截断了确切的 cookie 信息):

     [HTTP/1.1 200 OK]
p3p ["",""]
x-frame-options [SAMEORIGIN]
Date [Tue, 08 May 2012 15:01:40 GMT]
Vary [Accept-Encoding,User-Agent]
Transfer-Encoding [chunked]
Set-cookie [""=""; path=/; domain=""; expires=Tue, 01-Jan-2036 08:00:01 GMT]
Content-Type [text/html; charset=ISO-8859-1]
Server [Server]

最佳答案

请参阅 setReadTimeout 的文档:

Some non-standard implementation of this method ignores the specified timeout.

如果您认为这不是问题所在,请将超时设置为非常低的数字(例如 1)并进行检查。您应该得到一个 java.net.SocketTimeoutException 异常。

关于java urlconnection 不抛出 sockettimeoutexception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10489717/

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