简体   繁体   English

从HttpWebRequest / Response获取底层tcp连接

[英]Get underlying tcp connection from HttpWebRequest/Response

I am trying to get more information about what is going on when I connect to a website at a lower level than what HttpWebRequest and HttpWebResponse gives me. 我正在尝试获取有关当我连接到比HttpWebRequest和HttpWebResponse给我更低级别的网站时发生的事情的更多信息。 I am using C#. 我正在使用C#。

I would like to be able to see information about the dns lookup and the time it took to establish a connection (if a new connection was established). 我希望能够看到有关dns查找的信息以及建立连接所花费的时间(如果建立了新连接)。 HttpWebRequest and HttpWebResponse work at a higher level than this and I want to ask if there is a way of getting the underlying TcpClient object (or whatever low level object they use). HttpWebRequest和HttpWebResponse工作在比这更高的级别,我想询问是否有获取底层TcpClient对象(或他们使用的任何低级别对象)的方法。

If it is not possible, then is there a way to grab and manipulate a list of the connections that .net is maintaining without getting it through HttpWebRequest or HttpWebResponse? 如果不可能,那么有没有办法获取和操作.net正在维护的连接列表,而不通过HttpWebRequest或HttpWebResponse获取它?

I can't change the application I am working on to use TcpClient because it would be too time consuming to implement all the http stuff reliably. 我无法更改我正在使用TcpClient的应用程序,因为可靠地实现所有http内容会花费太多时间。

The best that I can get you is to create an app.config file with the following information: 我能得到的最好的是创建一个app.config文件,其中包含以下信息:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <system.diagnostics>
        <trace autoflush="true" />
            <sources>
                <source name="System.Net" maxdatasize="1024">
                    <listeners>
                        <add name="MyTraceFile"/>
                    </listeners>
                </source>
              <source name="System.Net.Sockets" maxdatasize="1024">
                    <listeners>
                        <add name="MyTraceFile"/>
                    </listeners>
                </source>  
           </sources>


            <sharedListeners>
                <add
                  name="MyTraceFile"
                  type="System.Diagnostics.TextWriterTraceListener"
                  initializeData="System.Net.trace.log"
                />
            </sharedListeners>
            <switches>
                <add name="System.Net" value="Verbose" />
              <add name="System.Net.Sockets" value="Verbose" />
            </switches>
    </system.diagnostics>
</configuration>

This will enable tracing and will kick out a log file named "System.Net.trace.log" in your app folder. 这将启用跟踪,并将在您的应用程序文件夹中启动名为“System.Net.trace.log”的日志文件。 You aren't going to get all of the information that you're looking for and its not easily consumable while the app is running but at least you don't need to have a third-party program running. 您不会获得所需的所有信息,并且在应用程序运行时不易消耗,但至少您不需要运行第三方程序。 Its not documented too much but there's some information out there at least. 它的记录不多,但至少有一些信息

使用Wireshark,这是找出所有内容的最佳方式。

如果不是Wireshark,那就使用Fiddler

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM