How can I use proxies for web requests in Flurl?

这一生的挚爱 提交于 2019-12-19 20:44:32

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/50649348/how-can-i-use-proxies-for-web-requests-in-flurl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!