通过 IHttpClientFactory 忽略 SSL 连接错误
Posted
技术标签:
【中文标题】通过 IHttpClientFactory 忽略 SSL 连接错误【英文标题】:Ignore SSL connection errors via IHttpClientFactory 【发布时间】:2019-07-31 05:37:46 【问题描述】:我在从我的 asp.net core 2.2 项目连接到像 there 这样的 https 站点时遇到问题。我使用 IHttpClientFactory 在 Startup.cs 中创建类型化的 HttpClient
services.AddHttpClient<ICustomService, MyCustomService>();
而且我不明白,如果不像这样手动创建 HttpClient,我怎么能忽略 SSL 连接问题
using (var customHandler = new HttpClientHandler())
customHandler.ServerCertificateCustomValidationCallback = (m, c, ch, e) => return true; ;
using (var customClient = new HttpClient(customHandler)
// my code
【问题讨论】:
【参考方案1】:使用ConfigureHttpMessageHandlerBuilder
:
services.AddHttpClient<ICustomService, MyCustomService>()
.ConfigureHttpMessageHandlerBuilder(builder =>
builder.PrimaryHandler = new HttpClientHandler
ServerCertificateCustomValidationCallback = (m, c, ch, e) => true
;
);
【讨论】:
以上是关于通过 IHttpClientFactory 忽略 SSL 连接错误的主要内容,如果未能解决你的问题,请参考以下文章