我的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
    }
}

最佳答案

使用ConfigureHttpMessageHandlerBuilder:

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

10-04 19:58