简体   繁体   English

Apache HttpClient(4.5)是否支持分块数据?

[英]Does Apache HttpClient (4.5) support chunked data?

I have a simple method to send a POST request: 我有一个简单的方法来发送POST请求:

public HttpResponse post(InputStream content, String contentType, URI url) {
    InputStreamEntity entity = new InputStreamEntity(content);
    entity.setChunked(true);
    entity.setContentType(contentType);

    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(entity)

    return httpClient.execute(httpPost, httpContext);
}

The httpPost seems well configured: httpPost似乎配置得很好:

  • httpPost.getEntity().toString() = [Content-Type: application/json,Chunked: true] httpPost.getEntity().toString() = [Content-Type:application / json,Chunked:true]
  • httpPost.getEntity().getContentLength() = -1 httpPost.getEntity().getContentLength() = -1

But the remote server receives Content-Length header 但远程服务器接收Content-Length标头

A request on http://httpbin.org/post shows the actual headers are: http://httpbin.org/post上的请求显示实际标题是:

"headers":{
    "Accept-Encoding": "gzip,deflate",
    "Content-Length": "571",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "blabla",
    "Via": "1.1 localhost (Apache-HttpClient/4.5.2 (cache))"
}

=> Does org.apache.http.client 4.5 really support chunked-encoding or does it fake it ? => org.apache.http.client 4.5是否真的支持chunked-encoding还是伪造它?

Thank you 谢谢

Chunked data is definitely supported by Apache HttpClient. Apache HttpClient肯定支持分块数据。

Httpbin.org relies on Nginx and I guess buffering of proxy's requests is enabled in their configuration. Httpbin.org依赖于Nginx,我想在他们的配置中启用了代理请求的缓冲。 Consequently, you do not see chunked transfer encoding in the result returned by httpbin. 因此,您在httpbin返回的结果中看不到分块传输编码。

Instead of using an external service such as httpbin.org for checking this kind of headers, use your own webserver. 不使用httpbin.org等外部服务来检查此类标头,而是使用您自己的网络服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM