Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 838
Zane Avatar asked Jan 03 '11 16:01

Zane


3 Answers

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.

like image 143
Chris Haas Avatar answered Nov 10 '22 02:11

Chris Haas


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

like image 30
fejesjoco Avatar answered Nov 10 '22 02:11

fejesjoco


If not Wireshark, then use Fiddler.

like image 1
John Saunders Avatar answered Nov 10 '22 03:11

John Saunders