8.2忽略 SSL 证书验证(https 错误)

创建于 2026 年 8 月 17 日约 1 分钟读完

若发送 HTTP 远程请求过程中出现 The SSL connection could not be established, see inner exception. 的证书错误问题,您可以通过添加以下配置来忽略 SSL 证书验证:

cs
// 默认客户端配置services.AddHttpClient(string.Empty)    .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler    {        // 忽略 SSL 证书验证        ServerCertificateCustomValidationCallback = HttpRemoteUtility.IgnoreSslErrors,        SslProtocols = HttpRemoteUtility.AllSslProtocols    });// 若使用 SocketsHttpHandler,可以通过以下配置来忽略 SSL 证书验证services.AddHttpClient(string.Empty)    .ConfigurePrimaryHttpMessageHandler(() => new SocketsHttpHandler()    {        SslOptions = new SslClientAuthenticationOptions        {            // 忽略 SSL 证书验证            RemoteCertificateValidationCallback = HttpRemoteUtility.IgnoreSocketSslErrors,            EnabledSslProtocols = HttpRemoteUtility.AllSslProtocols        },    });

除了可以在全局配置中忽略 SSL 证书验证外,您还可以通过 SetHttpClientProvider 方法为单次请求单独设置忽略 SSL 证书验证。示例代码如下:

cs
HttpRequestBuilder.Get("https://furion.net/")    .SetHttpClientProvider(() => (new HttpClient(new HttpClientHandler    {        // 忽略 SSL 证书验证        ServerCertificateCustomValidationCallback = HttpRemoteUtility.IgnoreSslErrors,        SslProtocols = HttpRemoteUtility.AllSslProtocols    }), client => client.Dispose()));