How to correctly specify proxy with System.Net.HttpClient

2.8k Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER

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>