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

后端 未结 8 709
伪装坚强ぢ
伪装坚强ぢ 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:47

    I know this is an old question, but I was in a tough spot where I didn't control the application config file, so I needed an easy way to enable the tracing via code and then easily access the raw request/response data in an event. So I put together this custom class, HttpRawTraceListener, which might be of use to others that are in my position:

    https://github.com/jhilgeman/HttpRawTraceListener/blob/master/HttpRawTraceListener.cs

    It was designed to be as simple as adding the file to your project, then calling:

    System.Diagnostics.HttpRawTraceListener.Initialize();
    

    ...to start the trace. From there, requests/responses will be parsed out of the trace messages and then be made available via the System.Diagnostics.HttpRawTraceListener.FinishedCommunication event.

    It's probably not 100% perfect for every scenario (e.g. it's not a proxy, so it won't capture web requests from a browser, for example), but it works pretty well for capturing HttpWebRequests requests/responses to web services, and it might be a good starting point if you need something like this.

提交回复
热议问题