Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android HttpClient issue setting Content-Length

I tried to create a post with the following...

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

but when I try to run the request I get the following error...

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

I am guessing this is because the content length is wrong.

like image 463
Jackie Avatar asked Jul 29 '26 13:07

Jackie


1 Answers

Apache HttpClient (even its fork shipped with Android) always calculates content length based on the properties of the enclosed HTTP entity. One does not need (and should not) set Content-Length and Transfer-Encoding headers manually.

like image 141
ok2c Avatar answered Aug 01 '26 02:08

ok2c



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!