我一直遇到java httpurlconnections,即使使用setReadTimeOut()
,也不会抛出sockettimeoutexception,并且它始终与最终(挂起2-3分钟后)早期EOF异常相关联。我在一些博客中读到这可能与使用BufferedReader的readLine方法读取有关,
字符串x ="&#34 ;;而((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]
答案 0 :(得分:0)
请参阅setReadTimeout的文档:
Some non-standard implementation of this method ignores the specified timeout.
如果您不认为这是问题,请将超时设置为非常低的数字,例如1并检查。您应该获得java.net.SocketTimeoutException
例外。