.Net 4.0 HttpClient usage?

2019-04-05 20:12发布

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

1条回答
疯言疯语
2楼-- · 2019-04-05 21:12

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:

HttpClient for .NET 4.0

MSDN: HttpClient

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
查看更多
登录 后发表回答