asp.net-web-api - 混淆Windows.Web.Http和System.Net.Http中使用哪个版本的HttpClient;

标签 asp.net-web-api windows-store-apps httpclient windows-8.1

我是 Web API 和 Windows Store App 8.1 的新手。我正在开发一个与 Web API 通信的 Windows 应用商店应用程序。当我尝试编写以下代码时:

// server:53452/api/demo?ReportingMonth=10&ReportingYear=2013" 

using (HttpClient httpClient = new HttpClient())
        {
            using (HttpResponseMessage response = await httpClient.GetAsync(new Uri("address")))
            {
                string result = await response.Content.ReadAsStringAsync();
                var prodt = JsonConvert.DeserializeObject<ObservableCollection<Statuses>>(result);
                return prodt;
            }
        }

我看到 HttpClient 位于 Windows.Web.Http 和 System.Net.Http 中。我应该使用哪个命名空间?

如果我选择 System.Net.Http 命名空间,当我尝试调用已启用 Windows 身份验证的 Web API 时,光标将不会返回到客户端,仍处于未知状态。不确定为什么我没有收到回复。

address = "abc.com:53452/api/demo?ReportingMonth=10&ReportingYear=2013"
using (HttpResponseMessage response = await httpClient.GetAsync(new Uri(address)))

如果我使用 Windows.Web.Http 中的 HttpClient,Windows 应用商店应用程序会要求我输入凭据,即使我正确输入了凭据,系统也会不断提示输入凭据。谁能解释一下为什么会发生这种情况?

最佳答案

Demystifying HttpClient APIs in the Universal Windows Platform

Which one should I use?

Since both of these APIs are available in UWP, the biggest question for HTTP developers is which one to use in their app. The answer is that it depends on a couple of factors:

  1. Do you need to integrate with native UI for collecting user credentials, control HTTP cache read and write behavior; or pass in a specific SSL client certificate for authentication?

If yes – then use Windows.Web.Http.HttpClient. At the time of this writing, the Windows.Web.Http API provides greater control over HTTP settings in UWP than the System.Net.Http API. In future versions, the System.Net.Http API may also be enhanced to support these features on UWP.

  1. Do you intend to write cross-platform .NET code (across UWP/ASP.NET 5/iOS and Android)?

If yes – then use System.Net.Http API. This allows you to write code that you can re-use on other .NET platforms such as ASP.NET 5 and .NET Framework desktop applications. Thanks to Xamarin, this API is also supported on iOS and Android, so you can reuse your code on these platforms as well.

关于asp.net-web-api - 混淆Windows.Web.Http和System.Net.Http中使用哪个版本的HttpClient;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21068115/

相关文章:

xml - 如何从 Web API 方法返回 Xml 数据?

c# - Swagger 在 API UI 屏幕上显示错误部分

asp.net-web-api - Asp.Net Core WebAPI CORS 不工作

c# - 为我的电子调查应用程序创建一个 .csv 文件。我该如何开始?

Angular:错误处理 - 拦截器和模态

Java HttpClient NoSuchAlgorithmException

c# - 找不到与 asp.net 网站项目中命名的 Controller 匹配的类型

windows-8 - XAML Windows 应用商店应用程序 - 在拖动过程中忽略 Slider.ValueChanged

windows - React native Windows - 应用程序认证工具包失败

Angular 4.3 HttpClient put 不触发