static Uri GetProxyUriFromScript(IntPtr 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) { 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; }
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); }
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 }