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);
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.
Generally, you don't want to dispose of HttpClient unless it's used very infrequently. Regular creation and disposal may lead to socket exhaustion.
Net. Http. HttpClient class sends HTTP requests and receives HTTP responses from a resource identified by a URI.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With