.Net 4.0 HttpClient 的用法? [英] .Net 4.0 HttpClient usage?
问题描述
我在 .Net 4.0 中并尝试使用 HttpClient.我读过一些文章说 4.0 不再支持它,但你仍然可以使用它?我已经包含了 System.Net.Http;
程序集,但它不允许我向 HttpClient
提供必要的参数.知道如何解决这个问题吗?
im in .Net 4.0 and attempting to use the HttpClient. I read some articles saying that it was no longer supported in 4.0 but that you could still use it? i've included the System.Net.Http;
assembly but it's not allowing me to provide the necessary params to the HttpClient
. Any idea how I could fix this?
我在错误发生的地方加粗了.
I've bolded where the errors are occuring.
using (HttpClient http = new **HttpClient("{0}/v1/dm/labels/{1}.xml", MI_API_URL**))
{
http.**TransportSettings**.Credentials = new NetworkCredential(apiusername, apipassword);
List<KeyValuePair<string, string>> parms = new List<KeyValuePair<string, string>>();
parms.Add(new KeyValuePair<string, string>("Status", "Wiped"));
HttpResponseMessage response = http.**Get**(new Uri("devices.xml", UriKind.Relative), parms);
response.EnsureStatusIsSuccessful();
responseoutput = response.Content.ReadAsString();
xdoc.LoadXml(responseoutput);
推荐答案
根据 MSDN,HttpClient 仅在 .NET Framework 4.5 中受支持.然而,有一个用于 .NET 4.0 的 HttpClient 实现.你可以在这里下载:
As per MSDN HttpClient is supported only in .NET Framework 4.5. Nevertheless there is an implementation of HttpClient for .NET 4.0. You can download it here:
在实现上仍然存在一些差异.例如,在 .NET 4.0 版本中,没有带有 2 个参数的构造函数.更多信息请查看源代码:
Still there are some differences in implementations. For example in version for .NET 4.0 there is no constructor w/ 2 parameters. Please see the source code for more information:
关于你的例子:
- impl 中没有带 2 个参数的构造函数.对于 .NET 4.0
- impl 中没有带有 2 个参数的 Get 方法.对于 .NET 4.0
- impl 中没有 TransportSettings 属性.对于 .NET 4.0
这篇关于.Net 4.0 HttpClient 的用法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!