3

我有一个简单的方法来发送POST请求:Apache HttpClient(4.5)支持分块数据吗?

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); 
} 

的httpPost似乎以及配置:

  • httpPost.getEntity().toString() = [内容类型:应用/ JSON,分块:真]
  • httpPost.getEntity().getContentLength() = -1

但远程服务器接收Content-Length

上​​请求显示实际标题有:

"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))" 
} 

=>是否org.apache.http.client 4.5真正支持分块编码或做它假呢?

谢谢

回答

1

分块的数据肯定是由Apache HttpClient支持的。

Httpbin.org依赖于Nginx,我猜想在它们的配置中启用了代理请求的缓冲。因此,您在httpbin返回的结果中看不到分块的传输编码。

而不是使用诸如httpbin.org的外部服务来检查这种类型的头文件,请使用您自己的网络服务器。