0

I asked how to set up a call to a service and got a great info on HttpClient. However, while that question's technically answered, I still get stuck.

In the console, I can see what request my browser send to the service to obtain the token for authorization. However, when I try to mimic the call building the request in my service layer, I get the following error message. The probability of me being at fault here is pretty steep. Not sure what to google for, really...

"StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:\u000d\u000a{\u000d\u000a Transfer-Encoding: chunked\u000d\u000a Connection: keep-alive\u000d\u000a Date: Wed, 12 Nov 2014 21:00:34 GMT\u000d\u000a Set-Cookie: lang=\"en\";Max-Age=31622400;expires=Fri, 13-Nov-2015 21:00:33 GMT;Path=/;Version=\"1\"\u000d\u000a Server: nginx/1.4.1\u000d\u000a Server: (Ubuntu)\u000d\u000a Content-Type: text/plain; charset=UTF-8\u000d\u000a}"

The call itself looks like this.

using (HttpClient client = new HttpClient())
{
  Task<HttpResponseMessage> message 
    = client.PostAsync(urlToken, new StringContent(credentials));
  message.Wait();
  result = message.Result.ToString();
}
Community
  • 1
  • 1
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • 500 error indicates something went wrong in the service. Check your web server logs and the event viewer to see if anything was logged there. – Tim Nov 12 '14 at 21:16

1 Answers1

1

As was posted in the answer that you accepted in your linked post, you need to read the content of the response. Calling ToString() directly on the response is not showing you the actual error.

Change your code to something like:

using (HttpClient client = new HttpClient())
{
  var response = await client.PostAsync(url, new StringContent(credentials));
  result = await response.Content.ReadAsStringAsync();
}

Once you can see the actual response message from the server, you should be able to figure out what to do next.

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
David Peden
  • 17,596
  • 6
  • 52
  • 72
  • I don't get the *Content* property from my intellisense. Also, *await* requires the whole method to be async. But I need it to be executed "in sync". That's why I go *message.Wait()*. Am I totally off here? – Konrad Viltersten Nov 12 '14 at 21:29
  • 1
    You don't see `.Content` because that property exists on `HttpResponseMessage`, not `Task`. You should see it on `message.Result`. As for your question about being totally off, I cannot say. You have presented too little context to know. Having said that, it is generally a bad practice to do what you're doing. See @stephencleary's blog post [on the subject](http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html). – David Peden Nov 12 '14 at 22:31