3

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.

Jackie
  • 21,969
  • 32
  • 147
  • 289

1 Answers1

5

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.

ok2c
  • 26,450
  • 5
  • 63
  • 71
  • Ok thanks guess that means there is another reason my post request isn't working. Is this documented somewhere? That may help future searchers. – Jackie Oct 12 '13 at 18:01
  • @oleg +1 My app was crashing when I was setting this in header of httpPost. Can you please lead me to such do's & dont's of HTTPclient, java? – Evol Gate Dec 26 '13 at 19:04
  • @EvolGate There is no compiled list do's and dont's, though, I guess it would be a great contribution to the project. Most of such details can however be found in the tutorial http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/index.html – ok2c Dec 27 '13 at 11:42