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;
}