Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

.net - Get HTTP requests and responses made using HttpWebRequest/HttpWebResponse to show in Fiddler

Is there any way I can hook Fiddler up to capture requests and responses made using .NET HttpWebRequest and HttpWebResponse?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The Fiddler FAQ gives the answer to this.

You essentially route your HTTP traffic through Fiddler (i.e. Use Fiddler as a proxy).

Here's some links that will help:
Fiddler Web Debugging - Configuring Clients

Which in turn links to here:
Take the Burden Off Users with Automatic Configuration in .NET

You can achieve this via some configuration settings in the web.config file (for an ASP.NET application) like so:

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

See here for complete details on the <defaultProxy> setting.

Alternatively, you can use a WebProxy object in your code using something like:

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 here for complete details on the WebProxy class.

Also note the important "caveat" that is mentioned in the Fiddler FAQ:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...