.net - 获取使用 HttpWebRequest/HttpWebResponse 发出的 HTTP 请求和响应以在 Fiddler 中显示

标签 .net httpwebrequest fiddler httpwebresponse

有什么方法可以让 Fiddler 捕获使用 .NET HttpWebRequest 和 HttpWebResponse 发出的请求和响应吗?

最佳答案

Fiddler 常见问题解答给出了答案。

您实际上是通过 Fiddler 路由 HTTP 流量(即使用 Fiddler 作为代理)。

这里有一些有用的链接:
Fiddler Web Debugging - Configuring Clients

这又链接到这里:
Take the Burden Off Users with Automatic Configuration in .NET

您可以通过 web.config 文件(对于 ASP.NET 应用程序)中的一些配置设置来实现此目的,如下所示:

<system.net>
  <defaultProxy>
    <proxy
      proxyaddress="http://[your proxy address and port number]"
      bypassonlocal="false"
    />
  </defaultProxy>
</system.net>

参见here有关 <defaultProxy> 的完整详细信息设置。

或者,您可以在代码中使用 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();

参见here有关 WebProxy 类的完整详细信息。

另请注意 Fiddler 常见问题解答中提到的重要“警告”:

Why don't I see traffic sent to http://localhost or 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.fiddler to hit localhost on the IPv4 adapter, or use http://ipv6.fiddler to 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.

关于.net - 获取使用 HttpWebRequest/HttpWebResponse 发出的 HTTP 请求和响应以在 Fiddler 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1470634/

相关文章:

asp.net-web-api - 手动组合请求时,WebAPI RequestBody 中的 QueryString 被截断?

c# - 无法修复 "Could not create SSL/TLS secure channel"发出 web 请求

带有空域的 HttpWebRequest cookie

c# - 如何像在 Ruby 中一样创建 c# .net 泛型列表?

c# - 伪造一个字符串引用

c# - 如何在c#中读取网站内容?

c# - Fiddler 看不到来自 C# HttpClient() 的 API 调用

https - Fiddler 根证书位置

c++ - .NET 4、C++、if...else 和 switch() 对性能的影响

.net - WriteBeginTag 的奇怪问题