private static unsafe Uri SafeDetectAutoProxyUrl(
            UnsafeNclNativeMethods.WinHttp.AutoDetectType discoveryMethod)
        {
            Uri autoProxy = null;

#if !FEATURE_PAL
            string url = null;

            GlobalLog.Print("NetWebProxyFinder::SafeDetectAutoProxyUrl() Using WinHttp.");
            SafeGlobalFree autoProxyUrl;
            bool           success = UnsafeNclNativeMethods.WinHttp.WinHttpDetectAutoProxyConfigUrl(discoveryMethod, out autoProxyUrl);
            if (!success)
            {
                if (autoProxyUrl != null)
                {
                    autoProxyUrl.SetHandleAsInvalid();
                }
            }
            else
            {
                url = new string((char *)autoProxyUrl.DangerousGetHandle());
                autoProxyUrl.Close();
            }

            if (url != null)
            {
                bool parsed = Uri.TryCreate(url, UriKind.Absolute, out autoProxy);
                if (!parsed)
                {
                    if (Logging.On)
                    {
                        Logging.PrintWarning(Logging.Web, SR.GetString(SR.net_log_proxy_autodetect_script_location_parse_error, ValidationHelper.ToString(url)));
                    }
                    GlobalLog.Print("NetWebProxyFinder::SafeDetectAutoProxyUrl() Uri.TryParse() failed url:" + ValidationHelper.ToString(url));
                }
            }
            else
            {
                if (Logging.On)
                {
                    Logging.PrintWarning(Logging.Web, SR.GetString(SR.net_log_proxy_autodetect_failed));
                }
                GlobalLog.Print("NetWebProxyFinder::SafeDetectAutoProxyUrl() DetectAutoProxyUrl() returned false");
            }
#endif // !FEATURE_PAL

            return(autoProxy);
        }
Esempio n. 2
0
        private static unsafe Uri SafeDetectAutoProxyUrl(UnsafeNclNativeMethods.WinHttp.AutoDetectType discoveryMethod)
        {
            Uri    result    = null;
            string uriString = null;

            if (ComNetOS.IsWinHttp51)
            {
                SafeGlobalFree free;
                if (!UnsafeNclNativeMethods.WinHttp.WinHttpDetectAutoProxyConfigUrl(discoveryMethod, out free))
                {
                    if (free != null)
                    {
                        free.SetHandleAsInvalid();
                    }
                }
                else
                {
                    uriString = new string((char *)free.DangerousGetHandle());
                    free.Close();
                }
            }
            else
            {
                StringBuilder autoProxyUrl = new StringBuilder(0x80a);
                if (UnsafeNclNativeMethods.WinInet.DetectAutoProxyUrl(autoProxyUrl, 0x80a, (int)discoveryMethod))
                {
                    uriString = autoProxyUrl.ToString();
                }
            }
            if (uriString == null)
            {
                if (Logging.On)
                {
                    Logging.PrintWarning(Logging.Web, SR.GetString("net_log_proxy_autodetect_failed"));
                }
                return(result);
            }
            if (!Uri.TryCreate(uriString, UriKind.Absolute, out result) && Logging.On)
            {
                Logging.PrintWarning(Logging.Web, SR.GetString("net_log_proxy_autodetect_script_location_parse_error", new object[] { ValidationHelper.ToString(uriString) }));
            }
            return(result);
        }