c# - 检测用于 HttpClient POST 或 GET 调用的 TLS 版本

标签 c# ssl

我正在尝试检索 TLS 版本信息。下面的代码使用 HttpClient 成功调用了 HTTP GET。我错过了什么?我从哪里获取来自 HttpClient 的 TLS 版本信息?

我正在做与 Which TLS version was negotiated? 中建议的相同的事情但这是特定于 WebRequest 的,它与 HttpClient 不同。

static async Task MainAsync()
{
    Uri baseURI = new Uri("https://jsonplaceholder.typicode.com/posts/1");
    string apiPath = "";
    using (var client = new HttpClient())
    {
        client.BaseAddress = baseURI;
        HttpResponseMessage response = await client.GetAsync(apiPath);
        Console.WriteLine("HTTP status code: " + response.StatusCode.ToString());
        GetSSLConnectionInfo(response, client.BaseAddress.ToString(), apiPath);
    }
    Console.ReadKey();
}

static async Task GetSSLConnectionInfo(HttpResponseMessage response, string baseURI, string apiPath)
{
    using (Stream stream = await response.RequestMessage.Content.ReadAsStreamAsync())
    {
        BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic;
        Stream CompressedStream = null;
        if (stream.GetType().BaseType == typeof(GZipStream))
        {
            CompressedStream = (GZipStream)stream;
        }
        else if (stream.GetType().BaseType == typeof(DeflateStream))
        {
            CompressedStream = (DeflateStream)stream;
        }

        var objbaseStream = CompressedStream?.GetType().GetProperty("BaseStream").GetValue(stream);
        if (objbaseStream == null)
        {
            objbaseStream = stream;
        }

        var objConnection = objbaseStream.GetType().GetField("m_Connection", bindingFlags).GetValue(objbaseStream);
        var objTlsStream = objConnection.GetType().GetProperty("NetworkStream", bindingFlags).GetValue(objConnection);
        var objSslState = objTlsStream.GetType().GetField("m_Worker", bindingFlags).GetValue(objTlsStream);
        SslProtocols b = (SslProtocols)objSslState.GetType().GetProperty("SslProtocol", bindingFlags).GetValue(objSslState);
        Console.WriteLine("SSL Protocol Used for " + baseURI + apiPath + System.Environment.NewLine + "The TLS version used is " + b);
    }
}

我期待 TLS 连接信息,但出现异常。

最佳答案

在引擎盖下 HttpClient 使用内部 TlsStream 类(如您的 WebRequest 示例)。我们只需要在另一个位置找到它。这是一个例子:

static void Main(string[] args)
{
    using (var client = new HttpClient())
    {
        using (var response = client.GetAsync("https://example.com/").Result)
        {
            if (response.Content is StreamContent)
            {
                var webExceptionWrapperStream = GetPrivateField(response.Content, "content");
                var connectStream = GetBasePrivateField(webExceptionWrapperStream, "innerStream");
                var connection = GetPrivateProperty(connectStream, "Connection");
                var tlsStream = GetPrivateProperty(connection, "NetworkStream");
                var state = GetPrivateField(tlsStream, "m_Worker");
                var protocol = (SslProtocols)GetPrivateProperty(state, "SslProtocol");
                Console.WriteLine(protocol);
            }
            else
            {
                // not sure if this is possible
            }
        }
    }
}

private static object GetPrivateProperty(object obj, string property)
{
    return obj.GetType().GetProperty(property, BindingFlags.Instance | BindingFlags.NonPublic).GetValue(obj);
}

private static object GetPrivateField(object obj, string field)
{
    return obj.GetType().GetField(field, BindingFlags.Instance | BindingFlags.NonPublic).GetValue(obj);
}

private static object GetBasePrivateField(object obj, string field)
{
    return obj.GetType().BaseType.GetField(field, BindingFlags.Instance | BindingFlags.NonPublic).GetValue(obj);
}

关于c# - 检测用于 HttpClient POST 或 GET 调用的 TLS 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54080785/

相关文章:

c# - 为什么我的 DateTime.Now.Millisecond 值始终为 "000"?

c# - Azure DevOps API 创建工作项返回 404 错误

javascript - Node.js + Express.js + SSL

java - 使用 Java 加密私钥

c# - 带有值转换器的动态数据模板

c# - QueueUserWorkItem() 和 BeginInvoke() 之间的区别是什么,用于执行不需要返回类型的异步事件

c# - 如何将对象数组作为单独的参数传递给方法?

firefox - 如何在 Firefox 中查看 key 交换协议(protocol)

ssl - ASIHTTPRequest 检索服务器证书

apache - 使用 Laravel 4 重定向到相对路径