GetDefaultProxy() публичный статический Метод

public static GetDefaultProxy ( ) : IWebProxy
Результат IWebProxy
Пример #1
0
        public static IWebProxy GetSystemWebProxy()
        {
#if MONOTOUCH
            return(CFNetwork.GetDefaultProxy());
#else
#if MONODROID
            // Return the system web proxy.  This only works for ICS+.
            var androidProxy = AndroidPlatform.GetDefaultProxy();
            if (androidProxy != null)
            {
                return(androidProxy);
            }
#endif
#if !NET_2_1
            if (IsWindows())
            {
                int iProxyEnable = (int)Microsoft.Win32.Registry.GetValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyEnable", 0);

                if (iProxyEnable > 0)
                {
                    string    strHttpProxy   = "";
                    bool      bBypassOnLocal = false;
                    ArrayList al             = new ArrayList();

                    string strProxyServer    = (string)Microsoft.Win32.Registry.GetValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyServer", null);
                    string strProxyOverrride = (string)Microsoft.Win32.Registry.GetValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", "ProxyOverride", null);

                    if (strProxyServer.Contains("="))
                    {
                        foreach (string strEntry in strProxyServer.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
                        {
                            if (strEntry.StartsWith("http="))
                            {
                                strHttpProxy = strEntry.Substring(5);
                                break;
                            }
                        }
                    }
                    else
                    {
                        strHttpProxy = strProxyServer;
                    }

                    if (strProxyOverrride != null)
                    {
                        string[] bypassList = strProxyOverrride.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                        foreach (string str in bypassList)
                        {
                            if (str != "<local>")
                            {
                                al.Add(str);
                            }
                            else
                            {
                                bBypassOnLocal = true;
                            }
                        }
                    }

                    return(new WebProxy(strHttpProxy, bBypassOnLocal, al.ToArray(typeof(string)) as string[]));
                }
            }
            else
            {
#endif
            if (Platform.IsMacOS)
            {
                return(CFNetwork.GetDefaultProxy());
            }

            string address = Environment.GetEnvironmentVariable("http_proxy");

            if (address == null)
            {
                address = Environment.GetEnvironmentVariable("HTTP_PROXY");
            }

            if (address != null)
            {
                try {
                    if (!address.StartsWith("http://"))
                    {
                        address = "http://" + address;
                    }

                    Uri       uri = new Uri(address);
                    IPAddress ip;

                    if (IPAddress.TryParse(uri.Host, out ip))
                    {
                        if (IPAddress.Any.Equals(ip))
                        {
                            UriBuilder builder = new UriBuilder(uri);
                            builder.Host = "127.0.0.1";
                            uri          = builder.Uri;
                        }
                        else if (IPAddress.IPv6Any.Equals(ip))
                        {
                            UriBuilder builder = new UriBuilder(uri);
                            builder.Host = "[::1]";
                            uri          = builder.Uri;
                        }
                    }

                    bool      bBypassOnLocal = false;
                    ArrayList al             = new ArrayList();
                    string    bypass         = Environment.GetEnvironmentVariable("no_proxy");

                    if (bypass == null)
                    {
                        bypass = Environment.GetEnvironmentVariable("NO_PROXY");
                    }

                    if (bypass != null)
                    {
                        string[] bypassList = bypass.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                        foreach (string str in bypassList)
                        {
                            if (str != "*.local")
                            {
                                al.Add(str);
                            }
                            else
                            {
                                bBypassOnLocal = true;
                            }
                        }
                    }

                    return(new WebProxy(uri, bBypassOnLocal, al.ToArray(typeof(string)) as string[]));
                } catch (UriFormatException) {
                }
            }
#if !NET_2_1
        }
#endif

            return(new WebProxy());
#endif // MONOTOUCH
        }