Android HttpClient问题设置Content-Length

Jac*_*kie 2 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
Run Code Online (Sandbox Code Playgroud)

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

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

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

ok2*_*k2c 5

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