【问题标题】:Ignore SSL connection errors via IHttpClientFactory通过 IHttpClientFactory 忽略 SSL 连接错误
【发布时间】: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
    }
}

【问题讨论】:

    标签: c# ssl asp.net-core httpclient


    【解决方案1】:

    使用ConfigureHttpMessageHandlerBuilder:

    services.AddHttpClient<ICustomService, MyCustomService>()
        .ConfigureHttpMessageHandlerBuilder(builder =>
        {
            builder.PrimaryHandler = new HttpClientHandler
            {
                ServerCertificateCustomValidationCallback = (m, c, ch, e) => true
            };
        });
    

    【讨论】:

      猜你喜欢
      • 2016-08-28
      • 2012-05-10
      • 2014-07-30
      • 1970-01-01
      • 2022-01-21
      • 2019-10-07
      • 1970-01-01
      • 2011-10-19
      • 2019-04-06
      相关资源
      最近更新 更多