How to configure web proxy for HttpClient created directly via HttpClientFactory?

15,553

Solution 1

When adding the client to the service collection you should be able to configure the handler there

Using the named client approach I'll use a constant to hold the client name.

public static class NamedHttpClients {
    public const string ProxiedClient = "ProxiedClient";
}

From there it is just a matter of configuring the client

//...
var serviceCollection = new ServiceCollection();

serviceCollection
    .AddHttpClient(NamedHttpClients.ProxiedClient)
    .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler() { 
        Proxy = httpProxy 
    });

var services = serviceCollection.BuildServiceProvider();

So that when calling the client via a resolved IHttpClientFactory

var httpClientFactory = services.GetService<IHttpClientFactory>();

var client = httpClientFactory.CreateClient(NamedHttpClients.ProxiedClient);

the returned client will use the handler with the proxy.

Solution 2

You can do something like this:

private HttpClient ClientFactory()
{
    var proxiedHttpClientHandler = new HttpClientHandler(){ UseProxy = true};
    proxiedHttpClientHandler.Proxy = new WebProxy("proxy address");
    var httpClient = new HttpClient(proxiedHttpClientHandler)
    {
        BaseAddress = new Uri("uri");
        Timeout = 2000; //if you need timeout;
    }
}
_createHttpClient = () => ClientFactory();

There's a good discussion here about using factory vs. manually instantiating an httpClient object.

Share:
15,553
Bruno Fiorentino
Author by

Bruno Fiorentino

Updated on June 14, 2022

Comments

  • Bruno Fiorentino
    Bruno Fiorentino almost 2 years
    • By directly I mean without Asp.Net Core IoC/DI helpers.

    I didn't find docs about it and I think my current solution isn't optimal because the handler lifecyle isn't managed by the HttpClientFactory:

    var proxiedHttpClientHandler = new HttpClientHandler() { Proxy = httpProxy };
    _createHttpClient = () => HttpClientFactory.Create(proxiedHttpClientHandler);
    

    Is there a better solution?

  • Bruno Fiorentino
    Bruno Fiorentino over 5 years
    I'm implementing a http dispatcher. It is called inside a message consuming loop and posts to webhooks. I'm assuming that I must use HttpClientFactory: docs.microsoft.com/en-us/dotnet/standard/…
  • Bruno Fiorentino
    Bruno Fiorentino over 5 years
    I'm not using Asp .Net Core IoC/DI and its helpers. I need to configure and consume HttpClientFactory directly.
  • Nkosi
    Nkosi over 5 years
    So how do you intent on creating the factory? You do know you can create the service collection yourself and get the benefits of the service provider?
  • Bruno Fiorentino
    Bruno Fiorentino over 5 years
    I'm not creating the service collection. That's the point. It's a console application template based project (dotnet new console), unrelated to Asp .Net Core. I need to use HttpClientFactory and HttpClient directly.
  • Bruno Fiorentino
    Bruno Fiorentino over 5 years
    That's ok if it is an option, but my question is about how to do it without the service collection IoC/DI infrastructure. It should not be hard.
  • Nkosi
    Nkosi over 5 years
    Ok understood. this is an example of console with DI. github.com/aspnet/HttpClientFactory/blob/master/samples/… i'll check to see if there is one without.