Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid HTTP POST Content-Length Exception
    primarykey
    data
    text
    <p>I have a simple program that sends an HTTP POST.</p> <p>It works <strong>only</strong> if the line <code>httpost.addheader("Content-Length","18")</code> is <strong>not</strong> present. Otherwise it fails. In the code, it's the line with "--->"<br> Commenting this line out makes the POST succeed.</p> <p>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.</p> <p>Any ideas why setting the Content-Length generates an exception?</p> <p>The code:</p> <pre><code>HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://test.com/a_post.php"); try { List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(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"); ----&gt; httppost.addHeader("Content-Length", "18"); ResponseHandler&lt;String&gt; 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(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload