HttpClient 4.1,InputStreamEntity,自动计算长度
在 HttpClient 3.1 中,我们有
InputStreamRequestEntity.CONTENT_LENGTH_AUTO
// The content length will be calculated automatically.
如何在4.1中为InputStreamEntity实现相同的效果?
In HttpClient 3.1 we had
InputStreamRequestEntity.CONTENT_LENGTH_AUTO
// The content length will be calculated automatically.
How can I achieve the same effect in 4.1 for InputStreamEntity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果处理 HTTP 1.1,则可以将 -1 作为长度。它将切换到(更有效的)分块编码,您不必显式指定内容长度;另外,您还可以获得其他好处。
您可以查看 http://en.wikipedia.org/wiki/Chunked_transfer_encoding 了解有关分块编码。
You can put -1 as length if you deal with HTTP 1.1. It will switch to (more efficient) chunked encoding where you don't have to specify the content length explicitly; plus you gain other benefits in addition.
You can check http://en.wikipedia.org/wiki/Chunked_transfer_encoding for more details on the chunked encoding.
你不能,因为他们在新版本中完全搞乱了 API。过去所做的是将整个流缓冲到内存中的字节数组中,所以现在您只需自己执行此操作并使用 ByteArrayEntity 代替。
看看这里的原始代码: http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/trunk/src/java/org/apache/commons/httpclient/methods/InputStreamRequestEntity.java?view=标记
第 125 行开始。
现在整个API就这样了,所有的易用性都没有了。也许这是更好、更安全的实现,但它是一个可怕的 API。
You can't 'cos they messed up the API completely in the new version. What that used to do was buffer the whole stream into a byte array in memory, so now you just do that yourself and use a ByteArrayEntity instead.
Look at the original code here: http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/trunk/src/java/org/apache/commons/httpclient/methods/InputStreamRequestEntity.java?view=markup
line 125 onwards.
The whole API is like that now, all the ease of use is gone. Maybe it is better, more safe implementation but it is horrible API.