c# - 使用 PostAsync、HttpClient 和 Json 从 C# Metro UI 客户端调用 MVC4 WebAPI 方法

标签 c# httpclient windows-8 microsoft-metro asp.net-mvc-4

我使用 MVC4 中的新 WebAPI 功能创建了一个方法,并让它在 Azure 上运行。该方法要求您发布一个简单的 LoginModel 对象,该对象由用户名和密码属性组成。是的,我计划在我越过这个减速带后进一步保护它 :-) 然后该方法以 Json 格式的对象响应:

enter image description here

我可以使用 Fiddler 成功调用此方法,前提是我在请求 header 中包含“Content-Type:application/json”。它返回 200,我可以进入 Fiddler Inspector 并查看 Json 响应对象就好了:

enter image description here

但是,我在使用 C#/XAML 从 Windows8 中的 MetroUI 应用程序调用相同方法时遇到问题。我开始使用 C# 中的 HttpClient 和新的 Async 概念,无论我如何格式化我的 Post 调用(即使明确指出我希望 Content-Type 为“application/json”),Fiddler 都会返回 500 错误并指出尝试使用的是 Content-Type:"text/html"。我相信这是问题的根源:

enter image description here

我已经尝试了所有可能的方法来发布到这个方法并取回 Json 对象,这是我最近的尝试:

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();
        });

这会导致 Content-Type 设置为“text/html”的 500 错误

这是另一个同样失败的尝试:

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();

谁能指出我正确的方向?

感谢 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();

它现在在我的请求 header 中显示“application/json”,但在 Web session 中仍然显示“text/html”:

enter image description here

最佳答案

试试这个来设置 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();

关于c# - 使用 PostAsync、HttpClient 和 Json 从 C# Metro UI 客户端调用 MVC4 WebAPI 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9863138/

相关文章:

javascript - 如何从 Javascript 检测 Win8/Metro

c# - 如何使用 MVVM 通过命令正确实现 TextChanged 事件

c# - 如何在字典中搜索值

c# - 使用 HttpClient 时避免从异步函数返回类型 Task<Object>

android - Xamarin AndroidClientHandler 发送客户端证书

windows-8 - 在哪里可以找到 Windows 8 上 Visual Studio 2012 中的设备面板?

c# - Windows 8 商店应用程序中的 Environment.GetCommandLineArgs

c# - 最小化WPF中的窗口?

c# - 日期时间重载

spring - 如何设计一个可以每秒处理1k请求的spring boot应用程序