28 March 2022 IE, proxy, HttpClient Robert Muehsig

Internet Explorer is - mostly - dead, but some weird settings are still around and “attached” to the old world, at least on Windows 10. If your system administrator uses some advanced proxy settings (e.g. a PAC-file), those will be attached to the users IE setting.

If you want to use this with a HttpClient you need to code something like this:

    string target = "https://my-target.local";
    var targetUri = new Uri(target);
    var proxyAddressForThisUri = WebRequest.GetSystemWebProxy().GetProxy(targetUri);
    if (proxyAddressForThisUri == targetUri)
        // no proxy needed in this case
        _httpClient = new HttpClient();
        // proxy needed
        _httpClient = new HttpClient(new HttpClientHandler() { Proxy = new WebProxy(proxyAddressForThisUri) { UseDefaultCredentials = true } });

The GetSystemWebProxy() gives access to the system proxy settings from the current user. Then we can query, what proxy is needed for the target. If the result is the same address as the target, then no proxy is needed. Otherwise, we inject a new WebProxy for this address.

Hope this helps!

Be aware: Creating new HttpClients is (at least in a server environment) not recommended. Try to reuse the same HttpClient instance!

Also note: The proxy setting in Windows 11 are now built into the system settings, but the API still works :)


Written by Robert Muehsig

Software Developer - from Saxony, Germany - working on this product. Microsoft MVP & Web Geek.
Other Projects: KnowYourStack.com | ExpensiveMeeting | EinKofferVollerReisen.de