c# - 如何在 Flurl 中使用代理进行 Web 请求?

标签 c# flurl

我使用 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();

最佳答案

我正在寻找类似的答案并发现了这个: https://github.com/tmenier/Flurl/issues/228

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

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

关于c# - 如何在 Flurl 中使用代理进行 Web 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50649348/

相关文章:

c# - getter 中的"new"关键字 > 性能受到影响?

c# - .NET 核心找不到 XmlDocument

c# - 如何在 python 中调试 win32com 调用

c# - 如何使用 Flurl 测试在内存中运行的 ASP.NET Web API?

asp.net-web-api - 我可以将 FlurlClient 与 Asp.Net Core TestServer 一起使用吗?

c# - 反序列化多维 JSON 字符串

c# - 如何在 C# 中将 List<string> 转换为 ReadOnlyCollection<string>

c# - 无法设置 Content-Type header

c# - 将文件添加到 Flurl 多部分 POST 请求时 IFormFileCollection 为空

azure - 如何从 Flurl 获取 Access_Token