public static bool TryCreate(out IWebProxy proxy)
        {
            // This will get basic proxy setting from system using existing
            // WinInetProxyHelper functions. If no proxy is enabled, it will return null.
            SafeWinHttpHandle sessionHandle = null;

            proxy = null;

            WinInetProxyHelper proxyHelper = new WinInetProxyHelper();

            if (!proxyHelper.ManualSettingsOnly && !proxyHelper.AutoSettingsUsed)
            {
                return(false);
            }

            if (proxyHelper.AutoSettingsUsed)
            {
                sessionHandle = Interop.WinHttp.WinHttpOpen(
                    IntPtr.Zero,
                    Interop.WinHttp.WINHTTP_ACCESS_TYPE_NO_PROXY,
                    Interop.WinHttp.WINHTTP_NO_PROXY_NAME,
                    Interop.WinHttp.WINHTTP_NO_PROXY_BYPASS,
                    (int)Interop.WinHttp.WINHTTP_FLAG_ASYNC);

                if (sessionHandle.IsInvalid)
                {
                    // Proxy failures are currently ignored by managed handler.
                    return(false);
                }
            }

            proxy = new HttpWindowsProxy(proxyHelper, sessionHandle);
            return(true);
        }
Example #2
0
 public static IWebProxy ConstructSystemProxy()
 {
     if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
     {
         return(HttpEnvironmentProxy.TryCreateWindows(out IWebProxy proxy) ? proxy : new HttpNoProxy());
     }
     else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
     {
         if (!HttpEnvironmentProxy.TryCreateWindows(out IWebProxy proxy))
         {
             if (HttpWindowsProxy.TryCreate(out proxy))
             {
                 return(proxy);
             }
         }
     }
     return(new HttpNoProxy());
 }