2012-07-23 221 views
8

im in .Net 4.0并尝试使用HttpClient。我阅读了一些文章,说它在4.0中不再被支持,但是你仍然可以使用它?我已经包含了System.Net.Http;组件,但它不允许我为HttpClient提供必要的参数。任何想法如何我可以解决这个问题?.net 4.0 HttpClient的用法?

我粗体显示错误发生的地方。

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

回答

15

根据MSDN HttpClient仅在.NET Framework 4.5中受支持。尽管如此,还是有一个用于.NET 4.0的HttpClient的实现。你可以在这里下载:

HttpClient for .NET 4.0

MSDN: HttpClient

还是有在实现一定的差异。例如在.NET 4.0的版本中,没有构造函数w/2参数。请参阅源代码以获取更多信息:

HttpClient for .NET 4.0 source code

关于你提到的例子:

  • 没有构造W的IMPL/2 PARAMS。对于.NET 4.0
  • 在impl中没有Get方法w/2个参数。对于.NET 4.0
  • impl中没有TransportSettings属性。对于.NET 4.0
+0

Thankyou拉曼,这是我怀疑。我用nuget抓住了dll – LinksTune 2012-07-23 16:02:48