Android HttpClient问题设置Content-Length

时间:2013-10-11 22:06:20

标签: android apache-httpclient-4.x androidhttpclient

我尝试使用以下内容创建帖子...

HttpPost httppost = new HttpPost(URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httppost.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
String length = String.valueOf(httppost.getEntity().getContentLength());
httppost.setHeader(HTTP.CONTENT_LEN, length); //If commented out it works

但是当我尝试运行请求时,我收到以下错误...

  

10-11 22:05:02.940:W / System.err(4203):org.apache.http.client.ClientProtocolException

我猜这是因为内容长度错误。

1 个答案:

答案 0 :(得分:5)

Apache HttpClient(甚至是Android附带的fork)总是根据所包含的HTTP实体的属性计算内容长度。一个人不需要(也不应该)手动设置Content-LengthTransfer-Encoding标头。