Published articles on other web sites*

Published articles on other web sites*

Getting Fiddler and the .NET Framework to Play (better) Together

I was working on a sample over this weekend and kept running into a wall related to Fiddler that was driving me crazy. Fortunately, I was able to find a solution thanks to Eric Lawrence and so I want to share that with you.

Here’s the scenario. I have a console application client which is using the HttpClient object model (from the Rest Starter Kit Preview 2) to call a WCF 4.0 service hosted on my local IIS Express at http://localhost:17697). As you can see, there’s nothing super fancy about how I’m calling my service:

static void AddNewProduct(ProductDto product) {
Console.WriteLine("Adding new product");
using(var client = new HttpClient("http://localhost:17697")) {
var content = HttpContentExtensions.CreateXmlSerializable(
product, Encoding.UTF8, "application/xml");
var response = client.Post("products", content);
Console.WriteLine("Product {0} now exists at location {1}",
product.id, response.Headers.Location);
}
}

The code works just fine by itself – the problems start when I want to insert Fiddler in the middle to monitor the HTTP traffic. Typically, this is done by either adding a “.” after localhost or using
the special hostname “ipv4.fiddler” (in fact, in IE9, you no longer need the “.” anymore – Fiddler will pick up localhost just fine). However, when I try both of these (plus one more – using “127.0.0.1.”), every request yields the following (though on a positive note, it does show up in fiddler)

HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
Date: Sat, 07 May 2011 22:13:40 GMT
Connection: close
Content-Length: 334

Bad Request - Invalid Hostname

It turns out that the solution is to add a rule in Fiddler to allow me to use a custom hostname on my client (one that Fiddler can pick up) and then have that Fiddler rule rename the host so that the request can be handled appropriately by my server and service.

To add this rule, open Fiddler’s rules file using the Rules -> Customize Rules menu option

fiddler-rules

Then, find the OnBeforeRequest function and add your rule in the function body – it’s signature looks like the following (I’m showing the full signature because you’ll need to add this function if it doesn’t already exist)

static function OnBeforeRequest(oSession:Fiddler.Session){
if (oSession.HostnameIs("myservice")) {
oSession.host = "localhost:17697"; }
}

I can now call my service using the hostname “myservice”

static void AddNewProduct(ProductDto product) {
Console.WriteLine("Adding new product");
using(var client = new HttpClient("http://myservice")) {
var content = HttpContentExtensions.CreateXmlSerializable(
product, Encoding.UTF8, "application/xml");
var response = client.Post("products", content);
Console.WriteLine("Product {0} now exists at location {1}",
product.id, response.Headers.Location);
}
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...