如何避免使用HttpURLConnection的HTTP HTTP响应的EOFException? [英] How to avoid EOFException for an empty HTTP response from HttpURLConnection?
问题描述
我发送一个HTTP请求到合法返回一个空白响应HTTP响应代码= 200(确定)的服务器。
I'm sending an HTTP request to a server which legitimately returns a blank response with HTTP response code = 200 (OK).
但这会导致标记行下面抛出一个 EOFException
:
But this causes the marked line below to throw an EOFException
:
InputStream responseStream = httpURLConnection.getInputStream();
final String contentEncoding = this.connection.getContentEncoding();
if ("gzip".equalsIgnoreCase(contentEncoding)) {
responseStream = new GZIPInputStream(responseStream); // throws EOFException
}
可能有一个明显的方法来防止这个,但我可以找不到我应该检查一下像 connection.getContentLength()> 0
或 responseStream.available()> 0
创建GZIPInputStream之前?这两个都不是正确的,我没有遇到任何类似的例子代码片段...
There may be an obvious way to prevent this but I can't find it. Should I perhaps be checking something like connection.getContentLength() > 0
or responseStream.available() > 0
before creating the GZIPInputStream? Neither of these seem quite right and I haven't come across anything similar in example code fragments...
推荐答案
$ b $我应该检查一下像connection.getContentLength()> 0
Should I perhaps be checking something like connection.getContentLength() > 0
是的。
或responseStream.available()> 0
or responseStream.available() > 0
绝对不是。可用()== 0不是EOF的有效测试,Javadoc明确表示。
Definitely not. available() == 0 isn't a valid test for EOF, and the Javadoc explicitly says so.
这篇关于如何避免使用HttpURLConnection的HTTP HTTP响应的EOFException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!