Use HttpClientFactory from .NET 4.6.2

All of the Microsoft.Extensions.* packages target .NET Standard 2.0. That means that you can use Dependency Injection, Configuration, Logging and HttpClientFactory if you add the appropriate package to your application. You need to add Microsoft.Extensions.Http to use HttpClientFactory and Microsoft.Extensions.Http.Polly if you want to use it with Polly To configure HttpClientFactory you’ll have to add … Read more

HttpClient.SendAsync using the thread-pool instead of async IO?

this.startRequest is a delegate that points to StartRequest which in turn uses HttpWebRequest.BeginGetResponse to start async IO. HttpClient is using async IO under the covers, just wrapped in a thread-pool Task. That said, note the following comment in SendAsync // BeginGetResponse/BeginGetRequestStream have a lot of setup work to do before becoming async // (proxy, dns, … Read more

How to use credentials in HttpClient in c#?

I had the exact same problem myself. It seems the HttpClient just disregards the credentials set in the HttpClientHandler. The following shall work however: using System.Net.Http.Headers; // For AuthenticationHeaderValue const string uri = “https://example.com/path?params=1”; using (var client = new HttpClient()) { var byteArray = Encoding.ASCII.GetBytes(“MyUSER:MyPASS”); var header = new AuthenticationHeaderValue( “Basic”, Convert.ToBase64String(byteArray)); client.DefaultRequestHeaders.Authorization = header; … Read more

Should HttpClient instances created by HttpClientFactory be disposed?

Calling the Dispose method is not required but you can still call it if you need for some reasons. Proof: HttpClient and lifetime management Disposal of the client isn’t required. Disposal cancels outgoing requests and guarantees the given HttpClient instance can’t be used after calling Dispose. IHttpClientFactory tracks and disposes resources used by HttpClient instances. … Read more

Deserialize JSON to Array or List with HTTPClient .ReadAsAsync using .NET 4.0 Task pattern

Instead of handcranking your models try using something like the Json2csharp.com website. Paste In an example JSON response, the fuller the better and then pull in the resultant generated classes. This, at least, takes away some moving parts, will get you the shape of the JSON in csharp giving the serialiser an easier time and … Read more

When or if to Dispose HttpResponseMessage when calling ReadAsStreamAsync?

So it seems like the calling code needs to know about and take ownership of the response message as well as the stream, or I leave the response message undisposed and let the finalizer deal with it. Neither option feels right. In this specific case, there are no finalizers. Neither HttpResponseMessage or HttpRequestMessage implement a … Read more