gpt4 book ai didi

c# - 通过 IHttpClientFactory 忽略 SSL 连接错误

转载 作者:太空宇宙 更新时间:2023-11-03 12:57:45 27 4
gpt4 key购买 nike

我在从我的 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
};
});

关于c# - 通过 IHttpClientFactory 忽略 SSL 连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57283898/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com