Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid DefaultHttpClient POST method Can't set Content Length, getting Http Error 411
    text
    copied!<p>I am having problem with an HttpPost on Android 4.2. I am trying to call an authentication service that is hosted in a .NET WebAPI service. </p> <p>The service requires that the request be made as a POST method, and that several custom request headers be supplied. The body of the request sends Json values to the service.</p> <p>Here is how I am composing my request;</p> <pre><code>HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://api.fekke.com/api/account/login"); httppost.addHeader(HTTP.TARGET_HOST, "api.fekke.com"); httppost.addHeader("Accept", "application/json"); httppost.addHeader("Fekke-AccessKey", "some-access-key"); httppost.addHeader("Date", dateString); httppost.addHeader("Fekke-Signature", "some-encoded-value"); httppost.addHeader("Content-Type", "application/json; charset=utf-8"); String jsonmessage = "{\"Username\": \"myusername\", \"Password\": \"mypassword987\"}"; httppost.setEntity(new StringEntity(jsonmessage, HTTP.UTF_8)); HttpResponse response = httpclient.execute(httppost); </code></pre> <p>I have tried to call this with a "Content-Length" header, and it throws an exception saying that the header already exists in the request.</p> <p>I have also tried calling this using an HttpURLConnection object, which what Google proposes using from this point forward, but this also runs into the same problem.</p> <p>I have been able to make this request call from the iOS and .NET without a problem, so I know it is not the service.</p> <h3>UPDATE #1</h3> <p>I ran the following example through a local version of the service, and I was able to isolate the error to when I pass a hashed value in one of the http headers. I am using a JWT security token that uses the HMAC SHA-256 hashing algorithm to set a value used by the service. This hashed value causes the execution of the request to blow up.</p> <h3>UPDATE #2</h3> <p>I was finally able to resolve the problem. The problem was caused by the Base64 encodeToString method adding an invisble character to the value. It was forcing a corruption in the header. I was using the default setting of 0 or Base64.DEFAULT. I changed the value to Base64.NO_WRAP, and it resolved the issue.</p>
 

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