.NET 4.0中HttpClient的使用情况如何? [英] .Net 4.0 HttpClient usage?
问题描述
即时通讯在.NET 4.0中,并尝试使用HttpClient的。我读过一些文章说,它已不再支持4.0,但你仍然可以使用它?我已经包含了 System.Net.Http;
组件,但它没有让我提供必要的PARAMS到的HttpClient
。任何想法如何,我可以解决这个问题?
我加粗那里的错误存在的。
使用(HttpClient的HTTP =新**的HttpClient({0} / V1 / DM /标签/ {1}的.xml,MI_API_URL **))
{
HTTP ** TransportSettings **凭证=新的NetworkCredential(apiusername,apipassword)。
名单< KeyValuePair<字符串,字符串>> PARMS =新的名单,其中,KeyValuePair<字符串,字符串>>();
parms.Add(新KeyValuePair<字符串,字符串>(状态,刮));
HTT presponseMessage响应= HTTP **获取**(新的URI(devices.xml,UriKind.Relative),PARMS)。
response.EnsureStatusIsSuccessful();
responseoutput = response.Content.ReadAsString();
xdoc.LoadXml(responseoutput);
根据MSDN的HttpClient只支持.NET框架4.5。不过有HttpClient的对.NET 4.0的实现。你可以在这里下载:
仍然有在实现一定的差异。例如,在版本.NET 4.0没有构造W / 2的参数。请参阅源$ C $ C的详细资料:
<一个href="http://wcf.$c$cplex.com/SourceControl/changeset/view/417c9a96fc24#WCFWebApi/Http/Src/Microsoft.Net.Http/HttpClient.cs">HttpClient对于.NET 4.0源$ C $ C
关于你提到的例子:
- 在没有构造函数中的W IMPL / 2 PARAMS。对于.NET 4.0
- 在没有获取方法W中实现了一套/ 2 PARAMS。对于.NET 4.0
- 有一个在实现了一套没有TransportSettings财产。对于.NET 4.0
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);
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:
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:
HttpClient for .NET 4.0 source code
Regarding your example:
- There is no constructor w/ 2 params in impl. for .NET 4.0
- There is no Get method w/ 2 params in impl. for .NET 4.0
- There is no TransportSettings property in impl. for .NET 4.0
这篇关于.NET 4.0中HttpClient的使用情况如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!