开发者

HttpClient: disabling chunked encoding

I am using the Apache Commons HttpClient along with Restlet to call a restful web service. Unfortunately, my server (ba开发者_如何学运维sed on Ruby on Rails) does not like the Transfer-Encoding: chunked that HttpClient is using by default.

Is there any way to disable the usage of chunked encoding for POSTs from the client?


As a general rule, for request not to be chunked, you need to specify exact size of post body, which for dynamically generated data means you need to buffer entire response in memory, see its size and only then send it.

Apache client documentation seems to confirm this: AbstractHttpEntity.setChunked() states

Note that the chunked setting is a hint only. If using HTTP/1.0, chunking is never performed. Otherwise, even if chunked is false, HttpClient must use chunk coding if the entity content length is unknown (-1).


As said in Restlet mailing list, in Restlet version 2.1, you can set ClientResource#entityBuffering property to true to cache content in memory and prevent chunked encoding.


The most reliable way, as @Slartibartfast hinted in his answer, is to explicitly switch HttpPost to HTTP 1.0 protocol.

  1. Set apache HttpPost request to HTTP 1.0 protocol (the same for HttpGet, if you need this...):

    HttpPost httpPost = new HttpPost(someUrl); httpPost.setProtocolVersion(HttpVersion.HTTP_1_0); // Since v.4.3 of Apache HttpClient

  2. When creating Multipart post request provide as an input for an attachment not an InputStream (as for HTTP 1.1, which causes chunked encoding), but an array of bytes, which you have to create from the same stream beforehand. This is why content length is known. See org.apache.http.entity.mime.MultipartEntityBuilder.addBinaryBody(String, byte[], ContentType, String)

I tested this for Android development, that required slightly different class names... (see https://github.com/andstatus/andstatus/issues/249 )

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜