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.
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.
Here is how I am composing my request;
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);
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.
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.
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.
UPDATE #1
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.
UPDATE #2
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.