How can I use proxies for web requests in Flurl?

前端 未结 1 1827
北海茫月
北海茫月 2021-01-18 07:11

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.<

相关标签:
1条回答
  • 2021-01-18 07:34

    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");
    });
    
    0 讨论(0)
提交回复
热议问题