【问题标题】:Does Apache HttpClient (4.5) support chunked data?Apache HttpClient (4.5) 是否支持分块数据?
【发布时间】:2017-07-06 21:53:36
【问题描述】:

我有一个发送 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);
}

httpPost 似乎配置良好:

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

但是远程服务器收到Content-Length header

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

=> org.apache.http.client 4.5 真的支持 chunked-encoding 还是伪造的?

谢谢

【问题讨论】:

    标签: java apache-httpclient-4.x chunked-encoding content-length


    【解决方案1】:

    Apache HttpClient 绝对支持分块数据。

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

    不要使用诸如 httpbin.org 之类的外部服务来检查此类标头,而是使用您自己的网络服务器。

    【讨论】:

      猜你喜欢
      • 2011-02-16
      • 2012-09-11
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-25
      • 1970-01-01
      相关资源
      最近更新 更多