Search code examples
asp.netweb-configdotnet-httpclient.net-4.7.1

How to correctly specify proxy with System.Net.HttpClient


Can anyone answer whether or not an HttpClient should be using default proxy if specified within web.config?

<system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true">
        <proxy proxyaddress="http://my.proxy" bypassonlocal="False" />
    </defaultProxy>
</system.net>

Whenever I use HttpClient I find myself having to implement a static HttpClientHandler

private static HttpClientHandler statichandler = new HttpClientHandler()
{
    Proxy = new WebProxy(ConfigurationManager.AppSettings["HttpClientProxy"].ToString()),
            UseProxy = true,
};

Is there a way to force httpclient to pickup system.net default config sections/What am I missing?


Solution

  • The actual solution was to implement Httpclient with HttpClientHandler, explicitly setting UseProxy to true.

    private static HttpClientHandler statichandler = new HttpClientHandler()
    {
       UseProxy = true
    };
    

    This then picked up the following:

    <system.net>
        <defaultProxy enabled="true" useDefaultCredentials="true">
            <proxy proxyaddress="http://my.proxy" bypassonlocal="False" />
        </defaultProxy>
    </system.net>