HttpClient.GetAsync with network credentials

Solution 1:

You can pass an instance of the HttpClientHandler Class with the credentials to the HttpClient Constructor:

using (var handler = new HttpClientHandler { Credentials = ... })
using (var client = new HttpClient(handler))
{
    var result = await client.GetAsync(...);
}

Solution 2:

You shouldn't dispose of the HttpClient every time, but use it (or a small pool of clients) for a longer period (lifetime of application. You also don't need the handler for it, but instead you can change the default headers.

After creating the client, you can set its Default Request Headers for Authentication. Here is an example for Basic authentication:

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "username:password".ToBase64());

ToBase64() represents a helper function that transforms the string to a base64 encoding.