Software Development

A journal for sharing all things software development related


Proxy HTTP Requests through Fiddler (.NET Core)

Fri, 28 Aug 2020 21:32 UTC by garethbrown

Without some non standard configuration, .NET does not route HTTP requests from the application / server through the Fiddler HTTP debugger, and seems to ignore the Local Area Network (LAN) Settings that Fiddler temporarily applies (see Internet Options in Control Panel).

Configure for HttpClient using HttpClientHandler options

This can be done by configuring an HttpClientHandler for the HttpClient:


// Note that HttpClient is intended to be instantiated once per application, rather than per-use.
// https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netcore-3.1

using (var client = new HttpClient(
    handler: new HttpClientHandler
    {
        // 8888 = Fiddler standard port
        Proxy = new WebProxy(new Uri("http://127.0.0.1:8888")),
        UseProxy = true
    }
))
{
    var result = client.GetAsync("https://bing.com").Result;
    Console.WriteLine(result.StatusCode);
}

Configure Globally using environment variables

Alternatively this can be done globally by setting the ALL_PROXY environment variable:

"ExampleApp": {
  ...
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development",
    "ALL_PROXY": "http://localhost:8888"
  },
  ...
}

Note that the ALL_PROXY setting seems a little 'sticky', in that it didn't appear to immediatley revert after a full rebuild and restart of the application, but I didn't spend long investigating this.

Additional proxy environment variable options can be found here:

https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.defaultproxy?view=netcore-3.1


The information on this site is provided “AS IS” and without warranties of any kind either
express or implied. To the fullest extent permissible pursuant to applicable laws, the author disclaims all warranties, express or implied, including, but not limited to, implied warranties of merchantability, non-infringement and suitability for a particular purpose.

UI block loader
One moment please ...