title: Detecting Windows proxy settings in .Net Core
body: <p>One of our test servers has proxy settings and I was able to detect what proxy to use by using a 1 line powershell command.</p>
  <pre><code> (new-object System.Net.WebClient).Proxy.GetProxy(&quot;https://www.google.com&quot;)
  </code></pre>
  <p>However, if I instantiate a WebClient object in .Net Core the proxy is null.  Our current solution is to simply set the proxy for all outgoing connections as part of the environment configuration.  This worked fine for some of our simpler services that only contacted one endpoint that needed to be proxied, however I am now working on a fix where one endpoint needs to be proxied and another not.</p>
  <p>My options are</p>
  <ol>
  <li>Add an exclusion list for endpoints not to be proxied</li>
  <li>Get the proxy settings from the operating system somehow</li>
  </ol>
  <p>This question is asking for help for number 2.  I can implement option 1 with no issue.</p>
  <p>My best guess is that the proxy information is not available due to changes in .Net architecture.  I found the following illuminating quote. (<a href="https://github.com/dotnet/runtime/issues/24756" rel="nofollow noreferrer">https://github.com/dotnet/runtime/issues/24756</a>)  <strong>We are using dot net core 3.</strong></p>
  <blockquote>
  <p>As of .NET Core 2.1, the HTTP protocol stack used in .NET Core was changed. It no longer uses native Windows WinHTTP nor Linux 'libcurl'. Those are considered now the 'legacy' stacks. We have a new HTTP protocol stack called &quot;SocketsHttpHandler&quot;. It used by default.</p>
  </blockquote>
  <p>So basically I think I am looking for some magic code to get proxy information from the old Windows network stack if running on Windows.</p>
  <pre><code>bool isWindows = System.Runtime.InteropServices.RuntimeInformation
                                                 .IsOSPlatform(OSPlatform.Windows);
  
  if (isWindows) {
     // Do magic get proxy settings secret sauce.
  }
  </code></pre>