Exemple #1
0
            static Uri GetProxyUriFromScript(NSString script, Uri targetUri)
            {
                CFProxy[] proxies = CFNetwork.GetProxiesForAutoConfigurationScript(script, targetUri);

                if (proxies == null)
                {
                    return(targetUri);
                }

                for (int i = 0; i < proxies.Length; i++)
                {
                    switch (proxies[i].ProxyType)
                    {
                    case CFProxyType.HTTPS:
                    case CFProxyType.HTTP:
                    case CFProxyType.FTP:
                        // create a Uri based on the hostname/port/etc info
                        return(GetProxyUri(proxies[i]));

                    case CFProxyType.SOCKS:
                    default:
                        // unsupported proxy type, try the next one
                        break;

                    case CFProxyType.None:
                        // no proxy should be used
                        return(targetUri);
                    }
                }

                return(null);
            }
            public Uri GetProxy(Uri targetUri)
            {
                if (targetUri == null)
                {
                    throw new ArgumentNullException("targetUri");
                }

                try
                {
                    CFProxySettings settings = CFNetwork.GetSystemProxySettings();
                    CFProxy[]       proxies  = CFNetwork.GetProxiesForUri(targetUri, settings);
                    Uri             uri;

                    if (proxies == null)
                    {
                        return(targetUri);
                    }

                    for (int i = 0; i < proxies.Length; i++)
                    {
                        switch (proxies[i].ProxyType)
                        {
                        case CFProxyType.AutoConfigurationJavaScript:
                            if ((uri = GetProxyUriFromScript(proxies[i].AutoConfigurationJavaScript, targetUri)) != null)
                            {
                                return(uri);
                            }
                            break;

                        case CFProxyType.AutoConfigurationUrl:
                            // unsupported proxy type (requires fetching script from remote url)
                            break;

                        case CFProxyType.HTTPS:
                        case CFProxyType.HTTP:
                        case CFProxyType.FTP:
                            // create a Uri based on the hostname/port/etc info
                            return(GetProxyUri(proxies[i]));

                        case CFProxyType.SOCKS:
                            // unsupported proxy type, try the next one
                            break;

                        case CFProxyType.None:
                            // no proxy should be used
                            return(targetUri);
                        }
                    }
                }
                catch
                {
                    // ignore errors while retrieving proxy data
                }
                // no supported proxies for this Uri, fall back to trying to connect to targetUri directly.
                return(targetUri);
            }
Exemple #3
0
            public Uri GetProxy(Uri targetUri)
            {
                NetworkCredential credentials = null;
                Uri proxy = null;

                if (targetUri == null)
                {
                    throw new ArgumentNullException("targetUri");
                }

                try {
                    CFProxySettings settings = CFNetwork.GetSystemProxySettings();
                    CFProxy[]       proxies  = CFNetwork.GetProxiesForUri(targetUri, settings);

                    if (proxies != null)
                    {
                        for (int i = 0; i < proxies.Length && proxy == null; i++)
                        {
                            switch (proxies[i].ProxyType)
                            {
                            case CFProxyType.AutoConfigurationJavaScript:
                                proxy = GetProxyUriFromScript(proxies[i].AutoConfigurationJavaScript, targetUri, out credentials);
                                break;

                            case CFProxyType.AutoConfigurationUrl:
                                // unsupported proxy type (requires fetching script from remote url)
                                break;

                            case CFProxyType.HTTPS:
                            case CFProxyType.HTTP:
                            case CFProxyType.FTP:
                                // create a Uri based on the hostname/port/etc info
                                proxy = GetProxyUri(proxies[i], out credentials);
                                break;

                            case CFProxyType.SOCKS:
                                // unsupported proxy type, try the next one
                                break;

                            case CFProxyType.None:
                                // no proxy should be used
                                proxy = targetUri;
                                break;
                            }
                        }

                        if (proxy == null)
                        {
                            // no supported proxies for this Uri, fall back to trying to connect to targetUri directly
                            proxy = targetUri;
                        }
                    }
                    else
                    {
                        proxy = targetUri;
                    }
                } catch {
                    // ignore errors while retrieving proxy data
                    proxy = targetUri;
                }

                if (!userSpecified)
                {
                    this.credentials = credentials;
                }

                return(proxy);
            }