I have a simple program that sends an HTTP POST.
It works only if the line httpost.addheader("Content-Length","18")
is not present. Otherwise it fails. In the code, it's the line with "--->"
Commenting this line out makes the POST succeed.
Android does not send the POST if that line is in the code and returns with a Protocol Exception error. I used Wireshark to verify that nothing is sent.
Any ideas why setting the Content-Length generates an exception?
The code: HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://test.com/a_post.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// DATA
nameValuePairs.add(new BasicNameValuePair("mydata", "abcde"));
nameValuePairs.add(new BasicNameValuePair("id","29"));
StringEntity se = new UrlEncodedFormEntity(nameValuePairs);
httppost.setEntity(se);
int seLength = (int) se.getContentLength();
String seLengthStr = Integer.toString(seLength);
httppost.addHeader("Content-Type","application/x-www-form-urlencoded");
----> httppost.addHeader("Content-Length", "18");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String httpResponse=httpclient.execute(httppost, responseHandler);
responseV.setText(responseV.getText()+ " " + httpResponse);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}