HttpClient. Default credentials to authenticate on auto discovered proxy

Viewed 3767

Control freak disclaimer: yes, there are a lot of questions about HttpClient and proxy, but they use at least one of

  • explicite (non default) proxy credentials
  • explicite (non WPAD) proxy configuration
  • deprecated Api, e. g. WebProxy.GetDefaultProxy, WebRequest and so on.

According to dotnet developers

if the default value of HttpClientHandler.Proxy is null, then HttpClientHandler will use IE proxy settings

So to set right corporate proxy with HttpClient we don't need any additional actions, just

_client = new HttpClient()

And there is no official way to get the proxy selected. But how can I pass default AD credentials to authenticate on auto discovered proxy then? I just receive "(407) Proxy Authentication Required."

P.S. Possibly there is another problem and running as a service under special account (especially on Linux) this code have no IE setting to use. So we need to re implement WPAD.

1 Answers

You can pass credentials to a default system proxy using code like this with HttpClient:

var handler = new HttpClientHandler();
handler.DefaultProxyCredentials = CredentialCache.DefaultCredentials;
var client = new HttpClient(handler);
Related