views:

961

answers:

5

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

+1  A: 

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.

RichieHindle
@Charming Anonymous Downvoter: Did you miss the bit where I said "If you can't"? Forgive me for trying to be helpful.
RichieHindle
@Richie: I neutralized the downvote as I also cannot understand why.
tuergeist
@tuergeist: Thank you. You're a gentleman.
RichieHindle
A: 

Maybe I don't understand your question, but Fiddler is a web debugger (proxy). If your requests were send through the proxy, it captures it.

Please describe, what you (really) want to do.

tuergeist
+4  A: 

Check this blog post I think thats what you want.

Carl Bergquist
+6  A: 

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.

CraigTP
thanks for all the detail
Dave
A: 

yes i gree with you .

salomgea