コード例 #1
0
ファイル: Forms.cs プロジェクト: zxyang178/Xamarin.Forms
            public void StartTimer(TimeSpan interval, Func <bool> callback)
            {
                NSTimer timer = null;

#if __UNIFIED__
                timer = NSTimer.CreateRepeatingScheduledTimer(interval, t =>
                {
#else
                timer = NSTimer.CreateRepeatingScheduledTimer(interval, () => {
                                #endif
                    if (!callback())
#if __UNIFIED__
                    { t.Invalidate(); }
#else
                    { timer.Invalidate(); }
                                                #endif
                });
                NSRunLoop.Main.AddTimer(timer, NSRunLoopMode.Common);
            }

            HttpClient GetHttpClient()
            {
                var proxy   = CFNetwork.GetSystemProxySettings();
                var handler = new HttpClientHandler();

                if (!string.IsNullOrEmpty(proxy.HTTPProxy))
                {
                    handler.Proxy    = CFNetwork.GetDefaultProxy();
                    handler.UseProxy = true;
                }
                return(new HttpClient(handler));
            }
コード例 #2
0
        public WebProxy GetProxySettings()
        {
            var systemProxySettings = CFNetwork.GetSystemProxySettings();

            var proxyPort = systemProxySettings.HTTPPort;
            var proxyHost = systemProxySettings.HTTPProxy;

            return(!string.IsNullOrEmpty(proxyHost) && proxyPort != 0
                ? new WebProxy($"{proxyHost}:{proxyPort}")
                : null);
        }
コード例 #3
0
            HttpClient GetHttpClient()
            {
                var proxy   = CFNetwork.GetSystemProxySettings();
                var handler = new HttpClientHandler();

                if (!string.IsNullOrEmpty(proxy.HTTPProxy))
                {
                    handler.Proxy    = CFNetwork.GetDefaultProxy();
                    handler.UseProxy = true;
                }
                return(new HttpClient(handler));
            }
コード例 #4
0
        public WebProxy GetWebProxy()
        {
            CFProxySettings systemProxySettings = CFNetwork.GetSystemProxySettings();
            string          proxyHost           = systemProxySettings.HTTPProxy;
            int             proxyPort           = systemProxySettings.HTTPPort;

            if (!string.IsNullOrWhiteSpace(proxyHost) &&
                proxyPort != 0)
            {
                return(new WebProxy(proxyHost, proxyPort));
            }

            return(null);
        }
コード例 #5
0
 public void WebProxy_Leak()
 {
     // note: needs to be executed under Instrument to verify it does not leak
     Assert.NotNull(CFNetwork.GetSystemProxySettings(), "should not leak");
 }
コード例 #6
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:
                                proxy = ExecuteProxyAutoConfigurationURL(proxies[i].AutoConfigurationUrl, targetUri, out credentials);
                                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);
            }