Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net 4.0 HttpClient usage?

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);
like image 993
AA11oAKas Avatar asked Jul 23 '12 15:07

AA11oAKas


People also ask

What is the use of HttpClient?

An HttpClient can be used to send requests and retrieve their responses. An HttpClient is created through a builder . The builder can be used to configure per-client state, like: the preferred protocol version ( HTTP/1.1 or HTTP/2 ), whether to follow redirects, a proxy, an authenticator, etc.

Should HttpClient be disposed C#?

Generally, you don't want to dispose of HttpClient unless it's used very infrequently. Regular creation and disposal may lead to socket exhaustion.

What is .NET HttpClient?

Net. Http. HttpClient class sends HTTP requests and receives HTTP responses from a resource identified by a URI.

Should I use WebClient or HttpClient?

You should use HttpWebRequest instead of HttpClient whenever you need the additional features that HttpWebRequest provides. Further, unlike WebClient, HttpClient lacks support for progress reporting and custom URI schemes. Although HttpClient doesn't support FTP, mocking and testing HttpClient is easier.


1 Answers

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
like image 194
Raman Zhylich Avatar answered Sep 21 '22 11:09

Raman Zhylich