跳到主要内容
ecdsa.com
.NET HttpClient.NET

错误信息

The SSL connection could not be established, see inner exception.

也可能输出为

  • System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.

从 .NET Core 3.x 到 .NET 10,该文本保持不变。框架(Refit、gRPC、Azure SDK)可能会再包装一层,因此内部异常可能在下面两三层。

.NET HttpClient — HttpRequestException wrapping the AuthenticationException or IOException of the TLS handshake

含义

这是 HttpClient 在连接时遇到任何 TLS 失败都会使用的通用包装。消息本身不包含任何诊断信息:真正的原因在 InnerException 中(指明证书问题的 AuthenticationException,或者来自传输层的 IOException)。只记录 e.Message 会把原因隐藏起来;应记录整个异常链。

原因

修复方法

  1. 1.

    记录完整的异常链

    逐层遍历 InnerException(或者记录整个异常对象,而不只是 Message),才能看到真正的原因。

    csharp
    catch (HttpRequestException e)
    {
        for (Exception? ex = e; ex != null; ex = ex.InnerException)
            logger.LogError("{Type}: {Message}", ex.GetType().FullName, ex.Message);
    }
  2. 2.

    根据内部消息找到对应原因

    每条内部消息都对应不同的修复方法;下方的相关条目会逐一介绍。

    text
    Inner exception                                                       -> look at
    "...errors in the certificate chain: UntrustedRoot / PartialChain"    -> trust store, server chain
    "...errors in the certificate chain: NotTimeValid"                    -> dates, clock
    "...validation procedure: RemoteCertificateNameMismatch"              -> host name vs SAN
    "Cannot determine the frame size or a corrupted frame was received."  -> port is not TLS
    "Received an unexpected EOF or 0 bytes from the transport stream."    -> server/proxy closed
    "SSL Handshake failed with OpenSSL error - SSL_ERROR_SSL."            -> protocol/cipher (Linux)

相关错误

浏览完整的签名错误库