HttpClient: disabling chunked encoding

浪尽此生 提交于 2019-12-11 02:40:27

问题


I am using the Apache Commons HttpClient along with Restlet to call a restful web service. Unfortunately, my server (based 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?


回答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.




回答2:


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).




回答3:


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 )



来源:https://stackoverflow.com/questions/7721554/httpclient-disabling-chunked-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!