Is there any way I can hook Fiddler up to capture requests and responses made using .NET HttpWebRequest and HttpWebResponse?
3 Answers
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.
If you can't, Wireshark is a similar tool that works at the network hardware level, so it can capture network traffic from any application.
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.
If you are able to modify the request URI, and it is localhost
then there is a much simpler solution: change the hostname to localhost.fiddler
.
This has no dependency on setting up proxies (whether setting HttpWebRequest.Proxy
or the <defaultProxy>
element in a .config
file).
(From comment on this question.)