12

I have a simple post request using the Flurl client, and I was wondering how to make this request using a proxy using information like the IP, port, username, and password.

string result = await atc.Request(url)
    .WithHeader("Accept", "application/json")
    .WithHeader("Content-Type", "application/x-www-form-urlencoded")
    .WithHeader("Host", "www.website.com")
    .WithHeader("Origin", "http://www.website.com")
    .PostUrlEncodedAsync(new { st = colorID, s = sizeID, qty = 1 })
    .ReceiveString();
MarredCheese
  • 17,541
  • 8
  • 92
  • 91
snacks
  • 123
  • 1
  • 4

1 Answers1

19

I was looking for a similar answer and found this: https://github.com/tmenier/Flurl/issues/228

Here is a copy of the contents of that link. It worked for me!

You can do this with a custom factory:

using Flurl.Http.Configuration;

public class ProxyHttpClientFactory : DefaultHttpClientFactory {
    private string _address;

    public ProxyHttpClientFactory(string address) {
        _address = address;
    }

    public override HttpMessageHandler CreateMessageHandler() {
        return new HttpClientHandler {
            Proxy = new WebProxy(_address),
            UseProxy = true
        };
    }
} 

To register it globally on startup:

FlurlHttp.Configure(settings => {
    settings.HttpClientFactory = new ProxyHttpClientFactory("http://myproxyserver");
});
MarredCheese
  • 17,541
  • 8
  • 92
  • 91
TinyRacoon
  • 4,655
  • 2
  • 23
  • 22
  • Would this affect all clients? What if you need to use proxy for https://x.com/endpoint and use it without proxy for https://y.com/endpoint? – Alek Davis Oct 03 '22 at 20:40
  • Yes it is a global Flurl setting so it would affect all connections. For mixing proxy and non: if Flurl doesn't support separate instances, you could try separate programs. – TinyRacoon Oct 05 '22 at 07:56