.net 获取使用 HttpWebRequest/HttpWebResponse 进行的 HTTP 请求和响应以在 Fiddler 中显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1470634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get HTTP requests and responses made using HttpWebRequest/HttpWebResponse to show in Fiddler
提问by Fung
Is there any way I can hook Fiddler up to capture requests and responses made using .NET HttpWebRequest and HttpWebResponse?
有什么方法可以让 Fiddler 连接起来捕获使用 .NET HttpWebRequest 和 HttpWebResponse 发出的请求和响应?
回答by CraigTP
The Fiddler FAQ gives the answer to this.
Fiddler FAQ 给出了答案。
You essentially route your HTTP traffic through Fiddler (i.e. Use Fiddler as a proxy).
您基本上通过 Fiddler 路由您的 HTTP 流量(即使用 Fiddler 作为代理)。
Here's some links that will help:
Fiddler Web Debugging - Configuring Clients
以下是一些有帮助的链接:
Fiddler Web 调试 - 配置客户端
Which in turn links to here:
Take the Burden Off Users with Automatic Configuration in .NET
依次链接到此处:
通过 .NET 中的自动配置减轻用户的负担
You can achieve this via some configuration settings in the web.config file (for an ASP.NET application) like so:
您可以通过 web.config 文件(对于 ASP.NET 应用程序)中的一些配置设置来实现这一点,如下所示:
<system.net>
<defaultProxy>
<proxy
proxyaddress="http://[your proxy address and port number]"
bypassonlocal="false"
/>
</defaultProxy>
</system.net>
See herefor complete details on the <defaultProxy>setting.
有关设置的完整详细信息,请参见此处<defaultProxy>。
Alternatively, you can use a WebProxy object in your code using something like:
或者,您可以使用以下内容在代码中使用 WebProxy 对象:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("[ultimate destination of your request]");
WebProxy myproxy = new WebProxy("[your proxy address]", false);
request.Proxy = myproxy;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
See herefor complete details on the WebProxy class.
有关WebProxy 类的完整详细信息,请参见此处。
Also note the important "caveat" that is mentioned in the Fiddler FAQ:
还要注意 Fiddler 常见问题中提到的重要“警告”:
Why don't I see traffic sent to http://localhostor http://127.0.0.1?
IE7 and the .NET Framework are hardcoded not to send requests for Localhost through any proxies, and as a proxy, Fiddler will not receive such traffic.The workaround is to use your machine name as the hostname instead of Localhost or 127.0.0.1. So, for instance, rather than hitting http://localhost:8081/mytestpage.aspx, instead visit http://machinename:8081/mytestpage.aspx.
...Or, if you're using Fiddler v2.1.8 or later, just use http://ipv4.fiddlerto hit localhost on the IPv4 adapter, or use http://ipv6.fiddlerto hit localhost on the IPv6 adapter. This works especially well with the Visual Studio test webserver (codename: Cassini) because the test server only listens on the IPv4 loopback adapter.
Lastly, you could Customize your Rules file like so:
static function OnBeforeRequest(oSession:Fiddler.Session) { if (oSession.HostnameIs("MYAPP")) { oSession.host = "127.0.0.1:8081"; } }...and then just hit http://myapp, which will act as an alias for 127.0.0.1:8081.
为什么我看不到发送到http://localhost或http://127.0.0.1 的流量?
IE7 和 .NET Framework 被硬编码为不通过任何代理发送对 Localhost 的请求,作为代理,Fiddler 不会收到此类流量。解决方法是使用您的机器名称作为主机名,而不是 Localhost 或 127.0.0.1。因此,例如, 不要访问http:// localhost:8081/mytestpage.aspx,而是访问 http://machinename:8081/mytestpage.aspx。
...或者,如果您使用 Fiddler v2.1.8 或更高版本,只需使用http://ipv4.fiddler在 IPv4 适配器上访问 localhost,或使用http://ipv6.fiddler在 IPv6 适配器上访问 localhost . 这对 Visual Studio 测试网络服务器(代号:Cassini)特别有效,因为测试服务器只侦听 IPv4 环回适配器。
最后,您可以像这样自定义规则文件:
static function OnBeforeRequest(oSession:Fiddler.Session) { if (oSession.HostnameIs("MYAPP")) { oSession.host = "127.0.0.1:8081"; } }...然后只需点击http://myapp,它将充当 127.0.0.1:8081 的别名。
回答by RichieHindle
If you can't, Wiresharkis a similar tool that works at the network hardware level, so it can capture network traffic from any application.
如果不能,Wireshark是一个类似的工具,可以在网络硬件级别工作,因此它可以捕获来自任何应用程序的网络流量。
Wireshark is a bit more complex than Fiddler, and more general, but it's a great tool to have in your toolbox, and worth investigating a bit of time into.
Wireshark 比 Fiddler 稍微复杂一些,也更通用,但它是您工具箱中的一个很好的工具,值得花一些时间研究。
回答by Richard
If you are able to modify the request URI, and it is localhostthen there is a much simpler solution: change the hostname to localhost.fiddler.
如果您能够修改请求 URI,localhost那么有一个更简单的解决方案:将主机名更改为localhost.fiddler.
This has no dependency on setting up proxies (whether setting HttpWebRequest.Proxyor the <defaultProxy>element in a .configfile).
这不依赖于设置代理(无论是设置HttpWebRequest.Proxy还是文件中的<defaultProxy>元素.config)。
(From comment on this question.)
(来自对这个问题的评论。)

