0

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();
}
dakshbhatt21
  • 3,558
  • 3
  • 31
  • 40
Tori
  • 1,358
  • 4
  • 19
  • 38
  • The stack trace says that the error is because the Content-Length Header is already present. Is that's the case, what's the method to set the Content-Length? – Tori Sep 29 '10 at 19:21
  • Why do you want to manually add a Content-Length, if it's already there? If you need to change it for some reason, it will then most likely be incorrect, causing a HTTP error. – TuomasR Sep 29 '10 at 19:50
  • In the HttpClient docs, it states that HttpClient sets the Content-length for you, so you don't have to. Having seen the source code, HttpClient actually checks to see if it has already been added manually, and throws the ProtocolException if it already exists – James Allen Jan 16 '11 at 15:36
  • i also have that problem, have you solved the problem? – crazywood Jul 30 '12 at 12:20

2 Answers2

1

Are you sure the content-length is exactly 18? My guess is that the request is not done if the code realizes that the set content-length is incorrect, as sending a request with an invalid content-length will (at least should) cause an error on the server.

Most likely if you omit the content-length, it is automatically added when required.

TuomasR
  • 2,296
  • 18
  • 28
  • The content is exactly 18, mydata=abcde&id=29. – Tori Sep 29 '10 at 20:12
  • Wireshark reports also the length as 18. Doing an httppost.getallheaders shows that there's 1 header when the content-length is not added and 2 headers when the content-length is added. – Tori Sep 29 '10 at 20:15
  • Yes, Apache HTTP Client adds the header for you, so there's no need to add it. Why it should otherwise cause a non-helpful exception though, I don't know. – Christopher Orr Sep 29 '10 at 20:27
  • Thanks, I will try it without adding the header and make sure that the web service does not complain of a lack of content-length header. – Tori Sep 29 '10 at 20:43
0

There are a number of conditions where HTTP clients and servers must not send the Content-Length header. It is often not required, including if the other side of the connection is HTTP/1.1-aware. It may be best simply to leave that header out, and let your HTTP client/server library handle the logic of whether to add the header.

yfeldblum
  • 65,165
  • 12
  • 129
  • 169
  • The web service I am accessing requires content-length, it wants the POST, CONTENT-TYPE, CONTENT-LENGTH and the SOAP packet. Then one has to rely on the Android HTTP implementation, to send the content-length. I will test this with the web service and report back. – Tori Sep 29 '10 at 21:18