HttpClient 4.1, InputStreamEntity, automatically calculated length
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?
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.
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.
精彩评论