1、C# .NET Core 使用HttpClient时忽略HTTPS证书
最近项目遇到HttpClient请求代理时报SSL认证失败,解决方案记录
var handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = delegate { return true; };
var client = new HttpClient(handler);
2、通过IHttpClientFactory忽略SSL连接错误
services.AddHttpClient("aliyun")
.ConfigureHttpMessageHandlerBuilder(builder =>
{
builder.PrimaryHandler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (m, c, ch, e) => true
};
});
3:基于IHttpClientFactory具体的实现
public async Task<string> GetApi(string url)
{
var httpClient = _httpClientFactory.CreateClient("aliyun");
var httpResponseMessage = await httpClient.GetAsync(url);
var content = await httpResponseMessage.Content.ReadAsStringAsync();
return content;
}