.Net 4.0 HttpClient usage?

旧城冷巷雨未停 提交于 2019-12-21 09:17:36

问题


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:


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


来源:https://stackoverflow.com/questions/11615853/net-4-0-httpclient-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!