c# - 使用代理支持对 HttpClient 执行基本身份验证
问题描述
我正在尝试从防火墙后面的服务器访问一些 Internet 资源。每次我提出请求时,我的代理都会收到错误消息。它说我没有经过身份验证(HTTP 407 错误代码)。但我正在使用下一个代码(我想它支持代理)
public class ProxyHttpClientFactory: HttpClientFactory {
private readonly ICredentials _credentials;
private readonly IWebProxy _proxy;
public ProxyHttpClientFactory(ProxyOptions options) {
_credentials = new NetworkCredential(options.Login, options.Password, options.ProxyUri);
_proxy = new WebProxy {
Address = new Uri(options.ProxyUri),
Credentials = _credentials
};
}
protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args) {
return new CustomHandler(_proxy, _credentials);
}
}
public CustomHandler(IWebProxy proxy, ICredentials credentials) {
UseProxy = true;
Proxy = proxy;
Credentials = credentials;
SslProtocols = SslProtocols.Tls12;
UseCookies = false;
ServerCertificateCustomValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>true;
}
解决方案
推荐阅读
- windows - 如何在powershell中使用short_command_name -args而不是python program_name -args?
- android - 即使预览看起来不错,Scrollview 也没有在模拟器或手机中显示任何内容 已解决
- python - AttributeError:模块“django.db.models”没有属性“模型”
- eclipse - Mac OX 上用于 Eclipse 的 TFS 插件作为视图或透视图不可见
- microsoft-teams - Teams 选项卡中的 OneNote 链接在移动设备中不起作用
- vba - 如何在多个工作表上运行 3 个循环以进行条件格式设置
- vb.net - 不确定如何使用 setter,Getter 工作正常
- php - html & php 表单到数据库输入 0000-00-00 作为日期而不是选择的 html5 datepicker 日期
- javascript - 打字稿 - 函数应该返回一个字符串而不是 () -> 字符串
- php - 尝试根据我的数据库记录自动选择选项