简体   繁体   English

如何在 Flurl 中为 Web 请求使用代理?

[英]How can I use proxies for web requests in Flurl?

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.我有一个使用 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();

I was looking for a similar answer and found this: https://github.com/tmenier/Flurl/issues/228我正在寻找类似的答案,发现这个: 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"); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM