作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个请求处理程序:
var httpClientHandler = new HttpClientHandler
{
Proxy = new WebProxy(proxy.Address, proxy.Port),
UseProxy = true
};
和:
var url = new Url(hostUrl)
.AppendPathSegment(pathSegment);
如何将请求处理程序添加到 FlurlClient?
最佳答案
创建您自己的 ProxiedHttpClientFactory
以覆盖 CreateMessageHandler()
方法:
public class ProxiedHttpClientFactory : DefaultHttpClientFactory
{
private readonly string _proxyAddress;
private readonly int _proxyPort;
public ProxiedHttpClientFactory(string proxyAddress, int proxyPort)
{
this._proxyAddress = proxyAddress;
this._proxyPort = proxyPort;
}
public override HttpMessageHandler CreateMessageHandler()
{
return new HttpClientHandler
{
Proxy = new WebProxy(this._proxyAddress, this._proxyPort),
UseProxy = true
};
}
}
然后使用它:
var settings = new FlurlHttpSettings
{
HttpClientFactory = new ProxiedHttpClientFactory("my.proxy.com", 8080)
};
var client = new FlurlClient(settings);
在现有的 Url
实例上:
var url = new Url(hostUrl)
.AppendPathSegment(pathSegment)
.ConfigureClient(settings => settings.HttpClientFactory = new ProxiedHttpClientFactory("my.proxy.com", 8080));
关于c# - 如何将 HttpClientHandler 添加到 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46391554/
我是一名优秀的程序员,十分优秀!