2012-07-23 258 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