How do I see the raw HTTP request that the HttpWebRequest class sends?

后端 未结 8 599
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 01:19

I know you are all going to answer \"use a debugging proxy server like Fiddler\" but it\'s not that simple.

Here\'s my situation: I have some code that runs on a ser

相关标签:
8条回答
  • 2020-11-29 01:23

    answering my own question here, because I thought of another way to do it. Basically the idea is -- you re-point the HttpWebRequest to a page that logs the incoming raw HTTP Request. In other words set up a custom HTTP handler as per this forum post:

    http://forums.asp.net/t/353955.aspx

    And then change just the URL in the HttpWebRequest to point to this new endpoint, but keep all other elements of the request the same. Write the result to a file or something and you're golden.

    0 讨论(0)
  • 2020-11-29 01:25

    You can use System.Net tracing mechanism to see the raw HTTP requests sent on the wire. You can also add your own tracelistener to the process.

    0 讨论(0)
  • 2020-11-29 01:33

    Another suggestion. Implement your own web proxy, and set your request to use it with WebRequest.Proxy. Then you should be able to extract the traffic from the proxy instance.

    Edit: update for links.

    0 讨论(0)
  • 2020-11-29 01:34

    I realise that this is an old question. @feroze's answer says what to do, but does not go into any detail on how to set up System.Net tracing to achieve it.

    As this question was the first Google result for my query into the subject, and as we are all busy people, I thought I would save you all from having to hunt down this information.

    System.Web is very powerful for debugging HttpWebRequests and can be easily set up using the web.config:

    <configuration>
        <system.diagnostics>
    
            <trace autoflush="true" /> 
    
            <sources>
                <source name="System.Net" maxdatasize="1024">
                    <listeners>
                        <add name="MyTraceFile"/>
                        <add name="MyConsole"/>
                    </listeners>
                </source>
            </sources>
    
            <sharedListeners>
                <add
                  name="MyTraceFile"
                  type="System.Diagnostics.TextWriterTraceListener"
                  initializeData="System.Net.trace.log" />
                    <add name="MyConsole" type="System.Diagnostics.ConsoleTraceListener" />
            </sharedListeners>
    
            <switches>
                <add name="System.Net" value="Verbose" />
            </switches>
    
        </system.diagnostics>
    </configuration>
    

    Adding a simple HttpWebRequest in your code, and running in debugging mode in Visual Studio, the following information will be displayed in the debug console:

    System.Net Verbose: 0 : [6596] WebRequest::Create(https://example.com/service.asmx)
    System.Net Verbose: 0 : [6596] HttpWebRequest#62063506::HttpWebRequest(https://example.com/service.asmx#11234)
    System.Net Information: 0 : [6596] RAS supported: True
    System.Net Verbose: 0 : [6596] Exiting HttpWebRequest#11234::HttpWebRequest() 
    System.Net Verbose: 0 : [6596] Exiting WebRequest::Create()     -> HttpWebRequest#11234
    System.Net Verbose: 0 : [6596] HttpWebRequest#11234 ::GetRequestStream()
    System.Net Verbose: 0 : [6596] ServicePoint#11234 ::ServicePoint(example.com:443)
    System.Net Information: 0 : [6596] Associating HttpWebRequest#11234with ServicePoint#11234
    System.Net Information: 0 : [6596] Associating Connection#11234 with HttpWebRequest#11234 
    System.Net Information: 0 : [6596] Connection#11234 - Created connection from x.x.x.x:xx to x.x.x.x:xx.
    System.Net Information: 0 : [6596] TlsStream#11234 ::.ctor(host=example.com, #certs=0)
    System.Net Information: 0 : [6596] Associating HttpWebRequest#11234 with ConnectStream#11234 
    System.Net Verbose: 0 : [6596] Exiting HttpWebRequest#11234 ::GetRequestStream()    -> ConnectStream#11234 
    System.Net Verbose: 0 : [6596] ConnectStream#7740977::Write()
    System.Net Verbose: 0 : [6596] Data from ConnectStream#11234::Write
    System.Net Verbose: 0 : [6596] 00000000 : 3C 73 6F 61 70 3A 45 6E-76 65 6C 6F 70 65 0D 0A : <soap:Envelope..
    ...etc
    

    I found this especially useful when trying to find out the cause of a webservice client error. It turned out I was missing a header.

    0 讨论(0)
  • 2020-11-29 01:35

    You say that you think .NET is lying to you, and the specific example you give is that the header Content-Length is missing from the HTTP response.

    But the header Content-Length is not required from an HTTP response. In fact, if the body of the response is in any dynamic, and if its length is not known in advance, then it is highly likely that the Content-Length header will be omitted!

    0 讨论(0)
  • 2020-11-29 01:44

    You can use a network traffic sniffer like wireshark.

    This is not a debugging proxy, but will sniff all traffic and let you see the raw requests/responses.

    0 讨论(0)
提交回复
热议问题