2

I have this request handler:

var httpClientHandler = new HttpClientHandler
{
    Proxy = new WebProxy(proxy.Address, proxy.Port),
    UseProxy = true
};

And:

var url = new Url(hostUrl)
    .AppendPathSegment(pathSegment);

How do I add the request handler to the FlurlClient?

Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263

1 Answers1

2

Create your own ProxiedHttpClientFactory that overrides the CreateMessageHandler() method:

public class ProxiedHttpClientFactory : DefaultHttpClientFactory
{
    private readonly string _proxyAddress;
    private readonly int _proxyPort;

    public ProxiedHttpClientFactory(string proxyAddress, int proxyPort)
    {
        this._proxyAddress = proxyAddress;
        this._proxyPort = proxyPort;
    }

    public override HttpMessageHandler CreateMessageHandler()
    {
        return new HttpClientHandler
        {
            Proxy = new WebProxy(this._proxyAddress, this._proxyPort),
            UseProxy = true
        };
    }
}

Then use it:

var settings = new FlurlHttpSettings
{
    HttpClientFactory = new ProxiedHttpClientFactory("my.proxy.com", 8080)
};

var client = new FlurlClient(settings);

And on an existing Url instance:

var url = new Url(hostUrl)
               .AppendPathSegment(pathSegment)
               .ConfigureClient(settings => settings.HttpClientFactory = new ProxiedHttpClientFactory("my.proxy.com", 8080));
haim770
  • 48,394
  • 7
  • 105
  • 133