如何获取HttpClient POST请求中使用的协商TLS版本 [英] How can I get negotiated TLS version used in HttpClient POST requests

查看:593
本文介绍了如何获取HttpClient POST请求中使用的协商TLS版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取在HttpClient SendAsync调用中使用的TLS版本信息。我已尝试通过此链接检测用于HttpClient POST或GET调用的TLS版本,但是当我在调试模式下运行它时,我发现在获取SslProtocol属性时会出现对象处置异常。有什么办法可以阻止.net在读取对象属性之前对其进行处理。

I am trying to get the TLS version information used in my HttpClient SendAsync calls. I have tried method from this link Detecting TLS Version used for HttpClient POST or GET calls but when I ran it in debug mode I have found that the object disposed exception comes while getting SslProtocol property. Is there any way that I can stop .net disposing the object properties until I read them.

以下是代码:

public bool ExecuteAsync()
{
    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
            }
        }
    }
return true;
}

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);
}

此外,我在上面的链接中以其他代码运行了该解决方案,并且该解决方案正在运行。我发现的区别是,当我在主线程中完成所有工作时,其他代码正在初始化用于执行SendAsync调用的新线程。我也在某处读到,使用 using不是很好的做法。

Also, I have run the solution in the above link in some other code and it is working. The difference that I have found is that the other code is initiating new thread for doing SendAsync calls while I am doing all my work in main thread. Also I read somewhere that it is not good practice to use "using" statement for SendAsync method as was used in the above link.

此外,是否还有其他方法可以获取TLS版本信息。我听说可以从.net框架日志中读取它,但是我不知道该怎么做。

Moreover, is there any other method to get the TLS version information. I have heard that it can be read from .net framework logs but I have no idea how to do that.

如果我可以获得与TLS流相关的其他信息,例如哈希算法和密码算法那挺棒的。另外,我正在使用.net Framework 4.6.1,对问题有影响吗?

If I can get other information related to TLS stream like hash algorithm and cipher algorithm that will be great. Also, I am using .net framework 4.6.1 will that have any impact on the problem?

推荐答案

我找到了一种方法借助此处给出的代码读取代码中的System.Net跟踪 System.Net(HttpWebRequest)跟踪而不使用文件或app.config?通过@MarkoW。

I found a method of reading System.Net traces in code with the help of the code given here System.Net (HttpWebRequest) tracing without using files or app.config? by @MarkoW.

我必须致电

ExecuteWithEnabledSystemNetLogging(SourceLevels webTraceSourceLevel, Action actionToExecute, params TraceListener[] listener).

对于我来说,输入变量在后面。

for my case the input variables were following.

webTraceSourceLevel是 SourceLevels.Information

webTraceSourceLevel is SourceLevels.Information

actionToExecute是我的方法,该方法发送我需要system.net跟踪的HttpClient请求,并将其分配给Action对象。

actionToExecute is my method which is sending HttpClient requests for which I need system.net tracing, assigned to Action object.

侦听器为 TextWriterTraceListener myTextListener = new TextWriterTraceListener(stream)

//其中stream是流对象与我通过的动作相关的system.net跟踪将会出现,然后我将其转换为字符串。

// where stream is a stream object in which my system.net tracing related to the action that I passed will come and later I converted it to string.

通过此操作,我获得了tls版本和其他与密码相关的信息。

With this I got the tls version and other cipher related information.

这篇关于如何获取HttpClient POST请求中使用的协商TLS版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆