3

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?

Jon Selby
  • 502
  • 1
  • 8
  • 23
  • Did you ever find a solution on your own? I'm working in an application which makes external http requests via several different libraries. WCF connections properly use the , yet HttpClient does not. – DannyMeister Sep 18 '18 at 17:18
  • I continued to use the solution as above. In an ideal world it would have used the default proxy.... most annoying. I wrapped around it a check to see if app setting was specified or not.... however if your using third party libraries you may hit a block. – Jon Selby Sep 18 '18 at 21:11
  • Actually the following post I believe answers the question, something I'll be referencing tomorrow. I just put a work around in place and you've prompted me to review. https://stackoverflow.com/questions/36803578/why-does-httpclienthandler-have-both-proxy-and-useproxy-properties – Jon Selby Sep 18 '18 at 21:22
  • Thanks, I ended up coming to the same conclusion. It looks like my edit to my comment didn't persist. I found that for some reason our code had set UseProxy to false. Set to true HttpClient respected the default proxy. – DannyMeister Sep 19 '18 at 16:12

1 Answers1

2

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>
Jon Selby
  • 502
  • 1
  • 8
  • 23