54

I'm troubleshooting a bug with a service I call through .NET's HttpClient, trying to route the request through a local proxy (Fiddler), but my proxy settings seem to not be taking effect.

Here's how I create the client:

private HttpClient CreateHttpClient(CommandContext ctx, string sid) {
    var cookies = new CookieContainer();

    var handler = new HttpClientHandler {
        CookieContainer = cookies,
        UseCookies = true,
        UseDefaultCredentials = false,
        Proxy = new WebProxy("http://localhost:8888", false, new string[]{}),
        UseProxy = true,
    };

    // snip out some irrelevant setting of authentication cookies

    var client = new HttpClient(handler) {
        BaseAddress = _prefServerBaseUrl,
    };

    client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));

    return client;
}

then I send the request by:

var response = CreateHttpClient(ctx, sid).PostAsJsonAsync("api/prefs/", smp).Result;

Request goes straight to the server without attempting to hit the proxy. What did I miss?

2
  • Is your service under test on your local machine? Consider proxy bypassing for local addresses.
    – Xaqron
    May 13, 2013 at 16:33
  • By the way, creating HttpClient instances is expensive. You should reuse instances whenever possible. There are a few blog posts that talk about this reachable using Google. May 13, 2016 at 2:07

3 Answers 3

55

This code worked for me:

using System.Net;
using System.Net.Http;

var httpClientHandler = new HttpClientHandler
{
    Proxy    = new WebProxy(Address: "http://localhost:8888", BypassOnLocal: false),
    UseProxy = true
}

Note that I am not using the WebProxy constructor overload that accepts string\[\] bypassList, but your code does - perhaps that's the problem?

2
  • Removing the string array worked for me too. I had to an entry to HOSTS to get the proxy to work on localhost. Nov 15, 2014 at 10:18
  • 1
    You just saved my day ^_^ Mar 17, 2017 at 8:04
10

Ah, The BaseAddress I was pointing to was http://localhost:8081. Turns out that despite setting BypassOnLocal to false, evidently localhost is still special enough that it bypasses the proxy.

I added a domain binding in IIS, host file entry to point that domain to 127.0.0.1, used newly created domain, now it works.

2
  • 2
    I'm getting a Bad Request - Invalid Host Name when I put the . after localhost
    – Michael
    May 16, 2013 at 16:01
  • @DarrelMiller I get no changes after i add localhost. <system.net> <defaultProxy> <proxy bypassonlocal="False" usesystemdefault="True" proxyaddress="http://localhost.:8888" /> </defaultProxy> </system.net>
    – felickz
    Aug 25, 2014 at 20:51
3

Is Fiddler configured to capture traffic from all processes? Look at the status bar where you see "Capturing". It should show "All Processes" next to it. If it shows "Web browsers", click it and change it to all processes. This could be different depending on the version of Fiddler you use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.