Search code examples
c#windows-phone-8internal-server-errordotnet-httpclientasynchttpclient

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


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?


Solution

  • POSTing a StringContent will set the content type to text/plain. You might find the server doesn't like that. Try to find out what media type the server is expecting and set the Headers.ContentType of the StringContent instance.