通过IHttpClientFactory忽略SSL连接错误

时间:2019-07-31 05:37:46

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

我的asp.net core 2.2项目与https站点(如there)之间的连接出现问题。我使用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 个答案:

答案 0 :(得分:4)

使用ConfigureHttpMessageHandlerBuilder

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