代码之家  ›  专栏  ›  技术社区  ›  eeeeaaii

如何查看httpwebrequest类发送的原始HTTP请求?

  •  60
  • eeeeaaii  · 技术社区  · 16 年前

    我知道你们都会回答“像小提琴一样使用调试代理服务器”,但并不是那么简单。

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

    问题是另一台服务器做了错误的事情,所以我希望能够将调试标志传递到页面中(通过查询字符串,例如?debug=true),以便打印出 完全未加工 HTTP请求它发送到另一个服务器,这样我就可以知道到底出了什么问题。此代码在多个地方运行,因此我希望能够在dev、staging或production上传递此标志,并且只查看请求,而不必弄清楚生产服务器是否可以与某个存在的代理服务器通信,等等。

    你会认为这样做很容易,对吗?所以我觉得自己很疯狂,但是我看了httpwebrequest及其父类webrequest的参考资料——什么都没有。不行。你会认为微软会想到这一点。最接近的是,您可以访问“headers”集合,但是当我尝试它时,它忽略了一些非常重要的头,如“content-length”,所以它对我来说一定是“欺骗”(我知道这是谎言,因为我知道远程服务器返回了200个状态——请求是成功的)ssful,只是返回错误/不同/错误的数据)

    下面是所要求的代码示例:

    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();
    
    8 回复  |  直到 7 年前
        1
  •  15
  •   Bill the Lizard    14 年前

    您可以使用System.NET跟踪机制查看在线路上发送的原始HTTP请求。您还可以将自己的TraceListener添加到进程中。

        2
  •  117
  •   Michael come lately PhiLho    7 年前

    我知道这是个老问题。 @feroze's answer 说明要做什么,但不详细介绍如何设置 System.Net 追踪以实现它。

    因为这个问题是我第一次查询这个主题的谷歌结果,而且我们都是忙碌的人,所以我想我可以帮你们所有人省去搜索这些信息的麻烦。

    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>
    

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

    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
    

    我发现这在试图找出WebService客户端错误的原因时特别有用。结果发现我少了一个头球。

        3
  •  5
  •   Oded    16 年前

    你可以使用网络流量嗅探器 wireshark .

    这不是调试代理,但会嗅探 全部的 并让您看到原始请求/响应。

        4
  •  4
  •   eeeeaaii    16 年前

    在这里回答我自己的问题,因为我想到了另一种方法。基本上,这个想法是——您将httpwebrequest重新指向一个记录传入的原始HTTP请求的页面。换句话说,根据论坛帖子设置自定义HTTP处理程序:

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

    然后只更改httpwebrequest中的url以指向这个新的端点,但保持请求的所有其他元素不变。把结果写在一个文件或什么东西上,你就是黄金。

        5
  •  1
  •   Community Mohan Dere    9 年前

    另一个建议。 Implement your own web proxy ,并将您的请求设置为与 WebRequest.Proxy . 然后您应该能够从代理实例中提取流量。

    编辑:更新链接。

        6
  •  1
  •   Mroczny Arturek    8 年前

    我建议你下载 Telerik Fiddler 捕捉进出交通。

    下面是一个简单的例子,如何使用该工具:

    1. 确保捕获流量已启用: enter image description here
    2. 打开浏览器并刷新页面,或者只通过HTTP客户端发送请求。 enter image description here
    3. 在切换到小提琴手之后,您应该看到您的请求: enter image description here
    4. 在顶部,尝试导航“原始”选项卡。 enter image description here
    5. 在下面的窗口中是您的原始请求 enter image description here
        7
  •  0
  •   yfeldblum    16 年前

    你说你认为.NET在骗你,你给出的具体例子是 Content-Length HTTP响应中缺少。

    但是标题 内容长度 不是HTTP响应所必需的。事实上,如果响应体处于任何动态状态,并且其长度事先未知,则很可能 内容长度 标题将被省略!

        8
  •  -1
  •   mike jwezorek    9 年前

    我一定遗漏了一些东西,因为以ascii文本的形式获取原始HTTP请求非常容易,只要您在page_in it()中获取它,page_load()就会有所不同。

    protected void Page_Init(object sender, EventArgs e)
    {
        //this gets the raw request as an ASCII String.
        byte[] biData = Request.BinaryRead(Request.TotalBytes);
        string sWholeRequestAsString = System.Text.Encoding.ASCII.GetString(biData);
    
    }