我如何看到HttpWebRequest类发送HTTP请求的原始? [英] How do I see the raw HTTP request that the HttpWebRequest class sends?

查看:139
本文介绍了我如何看到HttpWebRequest类发送HTTP请求的原始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你们都去回答使用调试代理服务器就像小提琴手,但它不是那么简单。

下面是我的情况:我有一台服务器上运行约code,在ASP.NET页面code-背后(aspx.cs),其中(除其他事项外)建立到另一个的服务器上,抓住一些东西,然后进行格式化并返回给浏览器。

问题是,其他服务器正在做错误的事情,所以我希望能够在调试标志传递到页面(通过查询字符串,如?调试=真正的),因此它会打印出的完全原始的HTTP请求,它发送到其他服务器,所以我可以看到底是什么错误。这code在几个地方跑,所以我希望能够只在这个标志传递的开发,分期,或生产,只是看看的要求,而不必弄清楚生产服务器是否能够谈一些代理服务器在某处存在,等等。

您可能会认为,这将是很容易做到这一点,对不对?所以我觉得我疯了还是什么,但是我看着为HttpWebRequest的参考和它的父类的WebRequest和 - 没有什么。没有做不到的。你可能会认为微软将有这个想法。最接近的是,你可以访问标题集合但是当我试了一下,略像内容长度一些真正重要的头 - 所以它必须是撒谎,我(我知道这是在说谎,因为我知道,一个事实,即远程服务器返回200状态 - 该请求是成功的,它只是返回坏/不同的/错误的数据)

下面是要求换code例如:

  HttpWebRequest的REQ =(HttpWebRequest的)WebRequest.Create(http://www.whatever.com);
req.Method = ...什么...;
...其他安装的请求......
/ *在这一点上,我们将要发送请求。
   什么是HTTP请求的原始模样? * /
HttpWebResponse RESP =(HttpWebResponse)req.GetResponse();


解决方案

您可以使用System.Net跟踪机制上看到线路发送的原始HTTP请求。您也可以添加自己的TraceListener的过程。

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 server, in an ASP.NET page code-behind (aspx.cs), which (among other things) establishes a connection to another server, grabs some stuff, and then formats it and returns it to the browser.

The problem is that the other server is doing the wrong thing, and so I want to be able to pass a debugging flag into the page (via the query string, e.g. ?debug=true) so that it will print out the completely raw HTTP request that it is sending to the other server so I can see what the heck is wrong. This code is running in several places so I want to be able to just pass in this flag on dev, staging, or production and just see the request, without having to figure out whether the production servers can talk to some proxy server that exists somewhere, etc.

You would think that it would be easy to do this, right? So I feel like I'm crazy or something but I looked at the reference for HttpWebRequest and its parent class WebRequest and -- nothing. No can do. You would think Microsoft would have thought of this. The closest thing is that you can access the "Headers" collection but when I tried it, it omitted some really important headers like "content length" -- so it must be "lying" to me (I know it's lying, because I know for a fact that the remote server is returning a 200 status -- the request is successful, it's just returning bad/different/wrong data)

Here is the asked-for code example:

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();

解决方案

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.

这篇关于我如何看到HttpWebRequest类发送HTTP请求的原始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆