eee*_*aii 64 c# asp.net debugging proxy httpwebrequest
我知道你们都会回答"使用像Fiddler这样的调试代理服务器",但事情并非那么简单.
这是我的情况:我有一些代码在服务器上运行,在ASP.NET页面代码隐藏(aspx.cs)中,其中(除其他外)建立与另一个服务器的连接,抓取一些东西,然后格式化它并将其返回给浏览器.
问题是其他服务器做错了,所以我希望能够将调试标志传递到页面中(通过查询字符串,例如?debug = true),这样它就会打印出完全原始的 HTTP请求它发送到其他服务器所以我可以看到什么是错的.这段代码在几个地方运行,所以我希望能够在dev,staging或production上传递这个标志,只看到请求,而不必弄清楚生产服务器是否可以与某处存在的代理服务器通信等
你会认为这样做很容易,对吧?所以我觉得我疯了或者别的什么,但我查看了HttpWebRequest及其父类WebRequest的参考资料 - 什么都没有.没有办法.你会认为微软会想到这一点.最接近的是你可以访问"Headers"集合,但是当我尝试它时,它省略了一些非常重要的标题,如"内容长度" - 所以它必须"撒谎"给我(我知道它在撒谎,因为我知道因为远程服务器返回200状态 - 请求成功,它只返回错误/不同/错误的数据)
这是ask-for代码示例:
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();
Run Code Online (Sandbox Code Playgroud)
kam*_*mui 130
我意识到这是一个老问题.@ feroze的回答说该怎么做,但没有详细说明如何设置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>
Run Code Online (Sandbox Code Playgroud)
HttpWebRequest
在代码中添加一个简单的代码,并在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
Run Code Online (Sandbox Code Playgroud)
我发现这在尝试找出Web服务客户端错误的原因时特别有用.原来我错过了一个标题.
在这里回答我自己的问题,因为我想到了另一种方法。基本上这个想法是——您将 HttpWebRequest 重新指向一个记录传入原始 HTTP 请求的页面。换句话说,根据此论坛帖子设置自定义 HTTP 处理程序:
http://forums.asp.net/t/353955.aspx
然后只更改 HttpWebRequest 中的 URL 以指向这个新端点,但保持请求的所有其他元素相同。将结果写入文件或其他东西,你就是金子。
归档时间: |
|
查看次数: |
67152 次 |
最近记录: |