エラーメッセージ
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)がさらにもう一度ラップすることがあるため、内部例外が 2〜3 階層下にある場合もあります。
.NET HttpClient — HttpRequestException wrapping the AuthenticationException or IOException of the TLS handshake
このエラーの意味
接続中に発生したあらゆる TLS の失敗を包む、HttpClient の汎用的なラッパーです。メッセージ自体には診断情報がなく、実際の理由は InnerException(証明書の問題を示す AuthenticationException、またはトランスポートからの IOException)にあります。e.Message だけをログに記録すると理由が隠れてしまうため、例外チェーン全体を記録してください。
発生する理由
証明書の検証に失敗した
よくある信頼されていないチェーンや不完全なチェーン、期限切れの証明書、ホスト名の不一致が該当します。内部の AuthenticationException に、X509ChainStatus または SslPolicyErrors の値が示されます。
共通のプロトコルまたは暗号がない
ときどきTLS 1.0/1.1 を使う古いサーバーや、時代遅れの暗号スイートを使うサーバーが該当します。Linux では、内部例外に OpenSSL の理由が示されます。
接続が TLS ではなかったか、切断された
ときどき平文 HTTP のポート、接続を閉じるプロキシやファイアウォール、ハンドシェイクをリセットするサーバーが該当します。
修正方法
- 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.
内部のメッセージを原因に対応付ける
内部のメッセージごとに対処法が異なります。下の関連項目で 1 つずつ取り上げています。
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)
関連するエラー
The remote certificate is invalid because of errors in the certificate chain: U….NET SslStream / HttpClientThe remote certificate is invalid according to the validation procedure: Remote….NET SslStream / HttpClientCannot determine the frame size or a corrupted frame was received..NET SslStream / HttpClientReceived an unexpected EOF or 0 bytes from the transport stream..NET SslStream / HttpClientSSL Handshake failed with OpenSSL error - SSL_ERROR_SSL..NET on Linux (SslStream over OpenSSL)