Get underlying tcp connection from HttpWebRequest/Response

江枫思渺然 提交于 2019-11-30 15:37:50

问题


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. I am using 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). 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).

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?

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.


回答1:


The best that I can get you is to create an app.config file with the following information:

<?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. 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.




回答2:


Use Wireshark, it's the best way to find out all that stuff.




回答3:


If not Wireshark, then use Fiddler.



来源:https://stackoverflow.com/questions/4586049/get-underlying-tcp-connection-from-httpwebrequest-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!