Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 855
Jon Selby Avatar asked Aug 10 '26 01:08

Jon Selby


1 Answers

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>
like image 76
Jon Selby Avatar answered Aug 13 '26 05:08

Jon Selby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!