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的外部服務來檢查這種類型的頭文件,請使用您自己的網絡服務器。