HttpURLConnection:必须读取整个响应有什么处理? [英] HttpURLConnection: What's the deal with having to read the whole response?

查看:185
本文介绍了HttpURLConnection:必须读取整个响应有什么处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的问题与此问题非常相似. /p>

我有一个downloadFile(URL)函数,该函数创建一个新的HttpURLConnection,将其打开,读取并返回结果.当我多次在同一个URL上调用此函数时,第二遍几乎总是返回-1的响应代码(但不会抛出异常!).

该问题的最高答案是非常有帮助,但是我想了解一些事情.

因此,如果将http.keepAlive设置为false可以解决问题,则表明到底是什么?服务器是否以违反http协议的方式进行响应?或更可能是我的代码以某种方式违反了协议?踪迹会告诉我什么?我应该找什么?

这是怎么回事:

您需要阅读所有错误信息 溪流.否则,它将 混淆下一个连接,那就是 -1的原因.

这是否意味着如果响应是某种类型的错误(那将是什么响应代码?),流将被完全读取吗?另外,每次尝试执行http请求时,我基本上都是在创建一个新连接,然后在末尾disconnect()进行连接.

但是,就我而言,我没有得到401或其他任何信息.它始终是200.但是我的第二个连接几乎总是失败.

这是否意味着我应该正在读取其他一些我没有读取的数据(以类似的方式,必须完全读取错误流)?

请帮助阐明这一点?我觉得我缺少一些基本的HTTP协议了解.

PS如果我仅使用Apache HttpClient,是否不必处理所有这些协议详细信息?它为我照顾一切吗?

解决方案

默认HTTP URL处理程序中对keep-alive的支持非常麻烦.我们总是将其关闭.

如果要保持活动状态,请使用Apache HttpClient和池化连接管理器.如果您不想更改代码,则可以得到另一个像这样的处理程序,

http://www.innovation.ch/java/HTTPClient/

如果第二个连接始终失败,则意味着您的服务器不支持keepalive.使用Keepalive,HTTP处理程序仅使连接保持打开状态(即使您调用断开连接).如果不支持keep-alive,但是服务器直到您对连接发出下一个请求时,处理程序才知道,所以服务器将关闭连接,因此第二个连接将失败.

关于读取错误流,它仅在收到非200响应时适用.

My current problem is very similar to this one.

I have a downloadFile(URL) function that creates a new HttpURLConnection, opens it, reads it, returns the results. When I call this function on the same URL multiple times, the second time around it almost always returns a response code of -1 (But throws no exception!!!).

The top answer in that question is very helpful, but there are a few things I'm trying to understand.

So, if setting http.keepAlive to false solves the problem, it indicates what exactly? That the server is responding in a way that violates the http protocol? Or more likely, my code is violating the protocol in some way? What will the trace tell me? What should I look for?

And what's the deal with this:

You need to read everything from error stream. Otherwise, it's going to confuse next connection and that's the cause of -1.

Does this mean if the response is some type of error (which would be what response code(s)?), the stream HAS to be fully read? Also, every time I am attempting an http request I am basically creating a new connection, and then disconnect()ing it at the end.

However, in my case I'm not getting a 401 or whatever. It's always a 200. But my second connection almost always fails. Does this mean there's some other data I should be reading that I'm not (in a similar manner that the error stream must be fully read)?

Please help shed some light on this? I feel like there's some fundamental http protocol understanding I'm missing.

PS If I were just using the Apache HttpClient, would I not have to deal with all these protocol details? Does it take care of everything for me?

解决方案

The support for keep-alive in the default HTTP URL handler is very buggy. We always turn it off.

Use Apache HttpClient with a pooled connection manager if you want keep-alive. If you don't want change your code, you can get another handler like this one,

http://www.innovation.ch/java/HTTPClient/

If your second connection always fails, that means your server doesn't support keepalive. With Keepalive, the HTTP handler simply leaves connection open (even if you call disconnect). The server closes connection if keep-alive is not supported but the handler doesn't know till you make next request on the connection so the 2nd connection fails.

Regarding the read error stream, it only applies if you get non-200 responses.

这篇关于HttpURLConnection:必须读取整个响应有什么处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆