调用使用PostAsync,HttpClient的&放从C#的Metro UI客户MVC4的WebAPI的方法; JSON [英] Calling MVC4 WebAPI methods from C# Metro UI Client using PostAsync, HttpClient & Json
问题描述
我创建使用MVC4新的WebAPI特征的方法,并让它在Azure上运行。该方法需要您发布,它由一个用户名和密码财产的简单LoginModel对象。是的,我计划进一步保证这一次我突破这个速度碰撞:-)的方法,然后用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:
我可以成功调用使用招这个方法,只要我有的内容类型:应用程序/ JSON的请求头。它返回200,我可以进入提琴手检查并查看JSON响应对象就好了:
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:
不过,我有打电话使用C#/ XAML在Windows8的一个MetroUI应用此方法同样的问题。我开始玩弄HttpClient的,并在C#和无论我如何格式化我的帖子调用(甚至明确地调用出来的时候,我想的Content-Type为应用程序/ JSON)提琴手与500错误返回新的异步概念并指出尝试使用内容类型是:text / html的。我beleive这是问题的根源:
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:
我要张贴这种方法并取回JSON对象已经尝试了一切可以想象的,这是我最近一次尝试:
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();
});
这导致500错误与内容类型设置为text / html的
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?
刚试过以下code感谢Nemesv的建议是:
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();
现在显示在我的请求头应用/ JSON,但仍显示在Web会话text / html的
It now shows "application/json" in my request header, but still shows "text/html" in the Web Session:
推荐答案
试试这个设置 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();
这篇关于调用使用PostAsync,HttpClient的&放从C#的Metro UI客户MVC4的WebAPI的方法; JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!