【问题标题】:How can I use proxies for web requests in Flurl?如何在 Flurl 中为 Web 请求使用代理?
【发布时间】:2018-06-01 18:41:35
【问题描述】:

我有一个使用 Flurl 客户端的简单发布请求,我想知道如何使用 IP、端口、用户名和密码等信息使用代理发出此请求。

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();

【问题讨论】:

    标签: c# flurl


    【解决方案1】:

    我一直在寻找类似的答案,结果发现: https://github.com/tmenier/Flurl/issues/228

    这是该链接内容的副本。它对我有用!

    您可以使用自定义工厂来做到这一点:

    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
            };
        }
    } 
    

    在启动时全局注册:

    FlurlHttp.Configure(settings => {
        settings.HttpClientFactory = new ProxyHttpClientFactory("http://myproxyserver");
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      • 2016-03-06
      相关资源
      最近更新 更多