Internal Server Error when doing a POST with HttpClient in Windows Phone 8

Nii Laryea picture Nii Laryea · Sep 7, 2013 · Viewed 20.7k times · Source

I am posting a string to a web server this way:

private async Task makeRequest(string url, string postData)
    {
        HttpClient client = null;
        HttpResponseMessage response = null;
        try
        {
            client = new HttpClient();
            response = await client.PostAsync(url, new StringContent(postData));

            response.EnsureSuccessStatusCode();
            Debug.WriteLine(response.StatusCode + " " + response.ReasonPhrase);
        }
        catch (HttpRequestException e)
        {
            Debug.WriteLine(e.Message);
        }
    }

But response.EnsureSuccessStatusCode(); throws a HttpRequestException. When I did a e.Message on the exception, it says : Response status code does not indicate success: 500 (Internal Server Error)..

What am I doing wrong that I get that error? And how do I correct it?

Answer

George Nikolaides picture George Nikolaides · Sep 7, 2013

Your best solution here, as a wise man said, in HTTP there is not magic.

Download Fiddler2, setup a proxy on the emulator to connect to it. Then run your code with HttpWebRequest save the header somewhere and then run it again with HttpClient.

Compare the two files carefully and fix whats needs to be fixed. Please note that even the smallest difference might matter.