//IWebProxy does not give access to the underlying WebProxy it returns type WebProxyWrapper //the underlying webproxy is needed in order to use the proxy address, the following uses reflecion to cast to WebProxy static WebProxy ConvertToWebProxy(IWebProxy proxyWrapper) { PropertyInfo propertyInfo = proxyWrapper.GetType().GetProperty("WebProxy", BindingFlags.NonPublic | BindingFlags.Instance); WebProxy wProxy = (WebProxy)propertyInfo.GetValue(proxyWrapper, null); return wProxy; }
//IWebProxy does not give access to the underlying WebProxy it returns type WebProxyWrapper //the underlying webproxy is needed in order to use the proxy address, the following uses reflecion to cast to WebProxy static WebProxy ConvertToWebProxy(IWebProxy proxyWrapper) { PropertyInfo propertyInfo = proxyWrapper.GetType().GetProperty("WebProxy", BindingFlags.NonPublic | BindingFlags.Instance); WebProxy wProxy = (WebProxy)propertyInfo.GetValue(proxyWrapper, null); return(wProxy); }
static void SimulateMonoGetProxy(Uri uri) { Console.WriteLine("# Mono - simulate Mono's WebRequest.DefaultWebProxy"); IWebProxy proxy = MonoWebRequest.DefaultWebProxy; if (proxy != null) { Console.WriteLine("MonoWebRequest.DefaultWebProxy.GetType: {0}", proxy.GetType().FullName); Uri proxyAddress = new Uri(proxy.GetProxy(uri).AbsoluteUri); if (string.Equals(proxyAddress.AbsoluteUri, uri.AbsoluteUri)) { Console.WriteLine("ProxyAddress matches request uri. Ignoring proxy uri: '{0}'", proxyAddress); return; } if (proxy.IsBypassed(uri)) { Console.WriteLine("Proxy IsByPassed for '{0}'", uri); return; } Console.WriteLine("Proxy found. Uri: '{0}'", proxyAddress); } else { Console.WriteLine("MonoWebRequest.DefaultWebProxy is null. Trying WebRequest.GetSystemWebProxy"); } }
internal static string DumpIWebProxy(IWebProxy proxy) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(" Type: " + ValidationHelper.ToString(proxy.GetType()) + "\r\n"); WebProxy webProxy = proxy as WebProxy; if (webProxy != null) { stringBuilder.Append(" - Address: " + ValidationHelper.ToString(webProxy._ProxyAddress) + "\r\n"); stringBuilder.Append(" - BypassProxyOnLocal: " + ValidationHelper.ToString(webProxy._BypassOnLocal) + "\r\n"); } stringBuilder.Append(" - -------------------------------------------------"); return(stringBuilder.ToString()); }
/// <summary> /// Return true or false if connecting through a proxy server /// </summary> /// <param name="uri"></param> /// <returns></returns> static bool IsSystemProxySet(Uri uri) { CheckProxyConfigSettings(); CheckMacProxy(uri); Console.WriteLine("# Mono's WebRequest"); Console.WriteLine("WebRequest.GetSystemWebProxy().GetType: {0}", originalSystemProxy.GetType().FullName); var systemProxy = GetSystemProxy(uri); Console.WriteLine("WebRequest.GetSystemWebProxy().GetProxy() returned proxy Uri: '{0}'", systemProxy.Address); // The reason for not calling the GetSystemProxy is because the object // that will be returned is no longer going to be the proxy that is set by the settings // on the users machine only the Address is going to be the same. // Not sure why the .NET team did not want to expose all of the useful settings like // ByPass list and other settings that we can't get because of it. // Anyway the reason why we need the DefaultWebProxy is to see if the uri that we are // getting the proxy for to should be bypassed or not. If it should be bypassed then // return that we don't need a proxy and we should try to connect directly. IWebProxy proxy = WebRequest.DefaultWebProxy; if (proxy != null) { Console.WriteLine("WebRequest.DefaultWebProxy.GetType: {0}", proxy.GetType().FullName); Uri proxyAddress = new Uri(proxy.GetProxy(uri).AbsoluteUri); if (string.Equals(proxyAddress.AbsoluteUri, uri.AbsoluteUri)) { Console.WriteLine("ProxyAddress matches request uri. Ignoring proxy uri: '{0}'", proxyAddress); return(false); } if (proxy.IsBypassed(uri)) { Console.WriteLine("Proxy IsByPassed for '{0}'", uri); return(false); } } else { Console.WriteLine("WebRequest.DefaultWebProxy is null. Trying WebRequest.GetSystemWebProxy"); proxy = GetSystemProxy(uri); if (proxy == null) { Console.WriteLine("WebRequest.GetSystemWebProxy returned null"); } } return(proxy != null); }
/// <summary> /// 获取请求客户端 /// </summary> /// <param name="proxy"> web代理 </param> /// <returns> </returns> public static HttpClient GetClient(IWebProxy proxy) { if (proxy == null) //不使用代理的情况下 { return(_Client); } if (proxy.GetType() == _EmptyProxyType) { return(_EmptyProxyClient); } if (proxy == _DefaultWebProxy) { return(_DefaultWebProxyClient); } if (proxy == WebRequest.DefaultWebProxy) { return(_DefaultWebProxyClient = Create(_DefaultWebProxy = proxy)); } return(Create(proxy)); }
/// <summary> /// Inicializa la instancia del tipo <see cref="RestSharp.RestClient"/> que se utilza para enviar las solicitudes al servicio Aspen. /// </summary> private void InitializeClient() { this.restClient = new RestClient(this.endpoint.ToString().TrimEnd('/')) { Timeout = (int)this.timeout.TotalMilliseconds }; this.restClient.UseSerializer(() => JsonNetSerializer.Default); IWebProxy webProxy = ServiceLocator.Instance.WebProxy; if (webProxy.GetType() != typeof(NullWebProxy)) { this.restClient.Proxy = ServiceLocator.Instance.WebProxy; } JsonConvert.DefaultSettings = () => new JsonSerializerSettings { Formatting = Formatting.Indented, ReferenceLoopHandling = ReferenceLoopHandling.Ignore }; }
public static string GetSystemProxyAddress() { IWebProxy swp = HTTPWebRequest.GetSystemWebProxy(); if (swp == null) { return(string.Empty); } WebProxy proxy = swp.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic)[0].GetValue(swp) as WebProxy; if (proxy == null) { return(string.Empty); } Uri address = proxy.Address; if (address == null) { return(string.Empty); } return(address.ToString()); }
/// <summary> /// Inicializa la instancia del tipo <see cref="RestSharp.RestClient"/> que se utilza para enviar las solicitudes al servicio Aspen. /// </summary> protected void InitializeClient() { this.JwtEncoder = new JwtEncoder(this.algorithm, ServiceLocator.Instance.JwtJsonSerializer, this.urlEncoder); this.validator = new JwtValidator(ServiceLocator.Instance.JwtJsonSerializer, this.datetimeProvider); this.JwtDecoder = new JwtDecoder(ServiceLocator.Instance.JwtJsonSerializer, this.validator, this.urlEncoder, this.algorithm); this.RestClient = new RestClient(this.endpoint.ToString().TrimEnd('/')) { Timeout = (int)this.timeout.TotalMilliseconds }; this.RestClient.UseSerializer(() => JsonNetSerializer.Default); IWebProxy webProxy = ServiceLocator.Instance.WebProxy; if (webProxy.GetType() != typeof(NullWebProxy)) { this.RestClient.Proxy = ServiceLocator.Instance.WebProxy; } JsonConvert.DefaultSettings = () => new JsonSerializerSettings { Formatting = Formatting.Indented, ReferenceLoopHandling = ReferenceLoopHandling.Ignore }; }
internal static string DumpIWebProxy(IWebProxy proxy) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(" Type: " + ValidationHelper.ToString(proxy.GetType()) + "\r\n"); WebProxy webProxy = proxy as WebProxy; if (webProxy!=null) { stringBuilder.Append(" - Address: " + ValidationHelper.ToString(webProxy._ProxyAddress) + "\r\n"); stringBuilder.Append(" - BypassProxyOnLocal: " + ValidationHelper.ToString(webProxy._BypassOnLocal) + "\r\n"); } stringBuilder.Append(" - -------------------------------------------------"); return stringBuilder.ToString(); }
/// <summary> /// Logs information about the specified proxy object to the global logger /// </summary> /// <param name="proxyObject">Proxy object to log information about</param> /// <param name="name">Optional name for proxy object, in form Proxy {0} Details. If not specified, heading is Proxy Details.</param> public void LogProxyInfo(IWebProxy proxyObject, string name = null) { var logLines = new LogBuilder(); if (proxyObject != null) { var proxyActual = proxyObject as System.Net.WebProxy; if (proxyActual == null) { var webProxyField = proxyObject.GetType().GetField("webProxy", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public); if (webProxyField != null) { proxyActual = webProxyField.GetValue(proxyObject) as System.Net.WebProxy; } } if (proxyActual != null) { if (proxyActual.Address != null) { if (name == null) { logLines.AppendLine("Proxy Details"); } else { logLines.Append(String.Format("Proxy {0} Details", name)); } logLines.Append(String.Format(" Address = {0}", proxyActual.Address)); if (proxyActual.Address.IsDefaultPort) { logLines.Append(String.Format(" using Default port")); } else { logLines.Append(String.Format(" using port {0}", proxyActual.Address.Port)); } if (proxyActual.BypassProxyOnLocal) { logLines.Append(String.Format(" Local bypass ON")); } if ((proxyActual.BypassList != null) && (proxyActual.BypassList.Length > 0)) { logLines.Append(String.Format(" Bypass ({0} entries)", proxyActual.BypassList)); foreach (var currBypassEntry in proxyActual.BypassList) { logLines.Append(String.Format(" {0}", currBypassEntry)); } } if (proxyActual.UseDefaultCredentials) { logLines.Append(String.Format(" Using default credentials")); } } else { if (name == null) { logLines.Append(String.Format("Proxy: Not Configured")); } else { logLines.Append(String.Format("Proxy {0}: Not Configured", name)); } } } else { if (name == null) { logLines.Append(String.Format("Proxy: Unrecognized type {0}, cannot describe", proxyActual.GetType().Name)); } else { logLines.Append(String.Format("Proxy {0}: Unrecognized type {1}, cannot describe", name, proxyActual.GetType().Name)); } } } else { if (name == null) { logLines.Append(String.Format("Proxy: Not Configured", name)); } else { logLines.Append(String.Format("Proxy {0}: Not Configured", name)); } } DuoBaseHttpMod.LogEvent(logLines, System.Diagnostics.EventLogEntryType.Information); }
private static IWebProxy GetDefaultProxy_UsingOldMonoCode() { DefaultProxySection defaultProxySection = ConfigurationManager.GetSection("system.net/defaultProxy") as DefaultProxySection; if (defaultProxySection == null) { Console.WriteLine("DefaultProxySectionInternal: defaultProxySection is null returning GetSystemWebProxy()"); return(GetSystemWebProxy()); } ProxyElement proxy = defaultProxySection.Proxy; WebProxy webProxy; if (proxy.UseSystemDefault != 0 && proxy.ProxyAddress == (Uri)null) { Console.WriteLine("DefaultProxySectionInternal: defaultProxySection not being used"); IWebProxy systemWebProxy = GetSystemWebProxy(); if (!(systemWebProxy is WebProxy)) { Console.WriteLine("DefaultProxySectionInternal: return GetSystemWebProxy(). system proxy type: {0}", systemWebProxy.GetType().FullName); return(systemWebProxy); } webProxy = (WebProxy)systemWebProxy; } else { webProxy = new WebProxy(); } if (proxy.ProxyAddress != (Uri)null) { Console.WriteLine("DefaultProxySectionInternal: setting proxyAddress {0}", proxy.ProxyAddress); webProxy.Address = proxy.ProxyAddress; } if (proxy.BypassOnLocal != ProxyElement.BypassOnLocalValues.Unspecified) { webProxy.BypassProxyOnLocal = (proxy.BypassOnLocal == ProxyElement.BypassOnLocalValues.True); } foreach (BypassElement bypass in defaultProxySection.BypassList) { webProxy.BypassArrayList.Add(bypass.Address); } return(webProxy); }