Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 987
Fung Avatar asked Sep 24 '09 09:09

Fung


People also ask

How do I get HttpWebRequest response?

If you call the GetRequestStream method, you must use the GetResponse method to retrieve the response. If a WebException is thrown, use the Response and Status properties of the exception to determine the response from the server.

What is the difference between HttpWebRequest and WebRequest?

WebRequest is an abstract class. The HttpWebRequest class allows you to programmatically make web requests to the HTTP server.

What is HttpWebResponse?

This class contains support for HTTP-specific uses of the properties and methods of the WebResponse class. The HttpWebResponse class is used to build HTTP stand-alone client applications that send HTTP requests and receive HTTP responses.


2 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.

like image 178
CraigTP Avatar answered Sep 21 '22 18:09

CraigTP


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.

like image 43
RichieHindle Avatar answered Sep 24 '22 18:09

RichieHindle