asp.net-core - 具有多个代理的 HttpClient
问题描述
如何使用HttpClient
多个代理的管道?
可以通过以下方式处理单个代理HttpClientHandler
:
HttpClient client1 = new HttpClient(new HttpClientHandler()
{
Proxy = new WebProxy()
{
Address = new Uri($"http://{proxyIp}:{proxyPort}"),
BypassProxyOnLocal = false,
UseDefaultCredentials = false
}
});
我希望请求通过多个代理。
我已经尝试过这样的子DelegatingHandler
类化:
public class ProxyDelegatingHandler : DelegatingHandler
{
public ProxyDelegatingHandler(string proxyIp, int proxyPort):
base(new HttpClientHandler()
{
Proxy = new WebProxy()
{
Address = new Uri($"http://{proxyIp}:{proxyPort}"),
BypassProxyOnLocal = false,
UseDefaultCredentials = false
}
})
{
}
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
return base.SendAsync(request, cancellationToken);
}
}
并将列表传递给工厂,但它会引发异常,这可能是由于不正确的实现引起的ProxyDelegatingHandler
:
var handlers = new List<DelegatingHandler>();
handlers.Add(new ProxyDelegatingHandler(ip1, port2));
handlers.Add(new ProxyDelegatingHandler(ip2, port2));
HttpClient client = HttpClientFactory.Create(handlers.ToArray())
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, url);
var res = await client.SendAsync(requestMessage);
例外:
'DelegatingHandler' 列表无效,因为'CustomHandler' 的属性'InnerHandler' 不为空。参数名称:处理程序
相关文章:链接
解决方案
推荐阅读
- html - Jquery函数没有被触发
- php - 如何将 html 下拉菜单链接到 php xpath 查询到 xml 文件?
- spotfire - Spotfire - 需要根据成本中心和期间获取最新的覆盖率代码
- javascript - 在nodeJS中构建我的数据模型的最佳方法是什么
- ios - 在显示之前加载 ViewController 的 WebView
- javascript - 将 iframe 语言更改为其父语言解决方法
- unity3d - Unity 自定义语音识别
- html - HTML 导航栏颜色
- c# - 枚举的重载运算符
- php - 来自管理文件夹索引的 htaccess 设置请求以从基本文件夹子目录加载