c# - 如何查看 HttpWebRequest 类发送的原始 HTTP 请求?

标签 c# asp.net debugging proxy httpwebrequest

我知道你们都会回答“使用像 Fiddler 这样的调试代理服务器”,但这并没有那么简单。

这是我的情况:我有一些代码在服务器上运行,在 ASP.NET 页面代码隐藏 (aspx.cs) 中,它(除其他外)建立与另一个的连接服务器,抓取一些东西,然后将其格式化并返回给浏览器。

问题是其他服务器做错了,所以我希望能够将调试标志传递到页面中(通过查询字符串,例如?debug=true),以便打印出完全原始 HTTP 请求发送到另一台服务器,这样我就可以看到到底出了什么问题。这段代码在几个地方运行,所以我希望能够在开发、暂存或生产中传递这个标志,只看到请求,而不必弄清楚生产服务器是否可以与某处存在的代理服务器通信等

你会认为这样做很容易,对吧?所以我觉得我疯了或什么的,但我查看了 HttpWebRequest 及其父类 WebRequest 的引用 - 没有。没有办法。你会认为微软会想到这一点。最接近的是你可以访问“标题”集合,但是当我尝试它时,它省略了一些非常重要的标题,比如“内容长度”——所以它对我来说一定是“说谎”(我知道它在说谎,因为我知道因为远程服务器正在返回 200 状态——请求成功,它只是返回错误/不同/错误的数据)

这是要求的代码示例:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.whatever.com");
req.Method = ... whatever ...;
... other setup for the request ...
/* At this point we are about to send the request.
   What does the raw HTTP request look like? */
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

最佳答案

我意识到这是一个老问题。 @feroze's answer说明了要做什么,但没有详细说明如何设置 System.Net 跟踪以实现它。

由于这个问题是我查询该主题的第一个 Google 结果,而且我们都是忙碌的人,所以我想我可以让你们所有人不必去寻找这些信息。

System.Web 对于调试 HttpWebRequest 非常强大,可以使用 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>

在你的代码中添加一个简单的HttpWebRequest,并在Visual Studio中以 Debug模式运行,调试控制台中将显示以下信息:

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

我发现这在试图找出网络服务客户端错误的原因时特别有用。原来我少了一个标题。

关于c# - 如何查看 HttpWebRequest 类发送的原始 HTTP 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3808016/

相关文章:

c# - 数据绑定(bind) POCO 属性

c# - 使用 FFTWLib (C#) : Using fftw. dft_r2c_2d() 会导致 vshost.exe 崩溃,如果 vshost 未运行则主程序会崩溃

c# - C# 中的空合并和右关联 - 说明?

c# - 如何在C#中运行具有所需权限的bat文件

asp.net - Elmah - ASP.NET -> 多个连接字符串 -> 在代码中设置 SQL 错误日志连接字符串

matlab - 退出并继续 MATLAB 调试

.net - Debug.Assert(false) 不会在 win8 Metro 应用程序中触发

javascript - 使用导入模块无法在 Chrome 开发工具中看到监视表达式

c# - 使用 asp.net 成员(member)资格有哪些优点和缺点?

html - 如何在 Razor CheckBoxFor() 中设置 Bootstrap 复选框