Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling MVC4 WebAPI methods from C# Metro UI Client using PostAsync, HttpClient & Json

I've created a method using the new WebAPI features in MVC4 and have it running on Azure. The method requires that you post a simple LoginModel object that consists of a Username and Password property. Yes, I plan on securing this further once I get past this speed bump :-) The method then responds with an object in Json format:

enter image description here

I can successfully call this method using Fiddler, provided that I include "Content-Type: application/json" in the Request Headers. It returns with 200 and I can go into the Fiddler Inspector and view the Json response object just fine:

enter image description here

I am however having problems calling this same method from a MetroUI app in Windows8 using C#/XAML. I began playing around with the HttpClient and the new Async concepts in C# and no matter how I formatted my Post calls (even when explicitly calling out that I want the Content-Type to be "application/json") Fiddler returns with a 500 error and states that the attempt was using Content-Type:"text/html". I beleive this to be the root of the problem:

enter image description here

I have tried everything conceivable in order to post to this method and get back the Json object, here is my most recent attempt:

HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        HttpContent content = new StringContent(@"{ ""Username"": """ + Username.Text + @", ""Password"": """ + Password.Text + @"""}");

        client.PostAsync("http://myapi.com/authentication", content).ContinueWith(result =>
        {
            var response = result.Result;

            response.EnsureSuccessStatusCode();
        });

This results in a 500 error with Content-Type set to "text/html"

Here was another attempt which also failed:

HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.PostAsync("http://myapi.com/authentication", new StringContent(@"{ ""Username"": """ + Username.Text + @", ""Password"": """ + Password.Text + @"""}", Encoding.UTF8, "application/json"));
string statusCode = response.StatusCode.ToString();

Can anyone point me in the right direction?

Just tried the following code thanks to the advice of Nemesv:

HttpClient httpClient = new HttpClient();
        HttpContent content = new StringContent(@"{ ""Username"": """ + Username.Text + @", ""Password"": """ + Password.Text + @"""}");
        content.Headers.ContentType = new MediaTypeHeaderValue("application/json");


        HttpResponseMessage response = await httpClient.PostAsync("http://webapi.com/authentication", content);

        string statusCode = response.StatusCode.ToString();
        response.EnsureSuccessStatusCode();

It now shows "application/json" in my request header, but still shows "text/html" in the Web Session:

enter image description here

like image 838
INNVTV Avatar asked Mar 25 '12 19:03

INNVTV


2 Answers

There is some missing try this is working.

UserLoginModel user = new UserLoginModel { Login = "username", Password = "password" };
string json = Newtonsoft.Json.JsonConvert.SerializeObject(user);
HttpContent content = new StringContent(json);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:1066/api/");
HttpResponseMessage response = client.PostAsync("Authenticate", content).Result;

if (response.IsSuccessStatusCode)
{
  var result = response.Content.ReadAsAsync<ResultModel>().Result;
}
else
{
  return string.Empty;
}
like image 39
vast Avatar answered Oct 10 '22 05:10

vast


Try this to set the Headers.ContentType:

HttpClient httpClient = new HttpClient();
HttpContent content = new StringContent(@"{ ""Username"": """ + "etc." + @"""}");
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage response = 
    await httpClient.PostAsync("http://myapi.com/authentication", content);
string statusCode = response.StatusCode.ToString();
like image 121
nemesv Avatar answered Oct 10 '22 05:10

nemesv