我经常遇到 java httpurlconnections,即使使用setReadTimeOut()
,它也不会在应该抛出 sockettimeoutexception 时抛出异常,并且它始终与最终(挂起 2-3 分钟后)过早的 EOF 异常相关联。我在一些博客中读到,这可能与使用 BufferedReader 的 readLine 方法 String x = ""; 读取有关。而((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)
以下是响应标头(截断了确切的 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]