public void BypassArrayList() { Uri proxy1 = new Uri("http://proxy.contoso.com"); Uri proxy2 = new Uri("http://proxy2.contoso.com"); WebProxy p = new WebProxy(proxy1, true); p.BypassArrayList.Add("http://proxy2.contoso.com"); p.BypassArrayList.Add("http://proxy2.contoso.com"); Assert.AreEqual(2, p.BypassList.Length, "#1"); Assert.IsTrue(!p.IsBypassed(new Uri("http://www.google.com")), "#2"); Assert.IsTrue(p.IsBypassed(proxy2), "#3"); Assert.AreEqual(proxy2, p.GetProxy(proxy2), "#4"); p.BypassArrayList.Add("?^!@#$%^&}{]["); Assert.AreEqual(3, p.BypassList.Length, "#10"); try { Assert.IsTrue(!p.IsBypassed(proxy2), "#11"); Assert.IsTrue(!p.IsBypassed(new Uri("http://www.x.com")), "#12"); Assert.AreEqual(proxy1, p.GetProxy(proxy2), "#13"); // hmm... although #11 and #13 succeeded before (#3 resp. #4), // it now fails to bypass, and the IsByPassed and GetProxy // methods do not fail.. so when an illegal regular // expression is added through this property it's ignored. // probably an ms.net bug?? :( } catch (ArgumentException) { Assert.Fail("#15: illegal regular expression"); } }
public bool ShouldProxyBeBypassed(HttpProxySettings proxySettings, HttpUri url) { //We are utilizing the WebProxy implementation here to save us having to re-implement it. This way we use Microsofts implementation var proxy = new WebProxy(proxySettings.Host + ":" + proxySettings.Port, proxySettings.BypassLocalAddress, proxySettings.BypassListAsArray); return(proxy.IsBypassed((Uri)url)); }
/// <summary> /// Метод формирования нестандартного прокси из параметров /// </summary> /// <param name="uri"></param> /// <param name="addrDefinition"></param> /// <param name="bypassDefinition"></param> /// <returns></returns> public static WebProxy SelectCustomProxy(Uri uri, string addrDefinition, string bypassDefinition) { var defs = ProxyRecord.Parse(addrDefinition); var def = defs.FirstOrDefault(_ => _.TargetScheme.ToUpperInvariant() == uri.Scheme.ToUpperInvariant()); if (null == def) { def = defs.FirstOrDefault(_ => _.TargetScheme == "*"); } if (null == def) { return(null); } var proxy = new WebProxy( def.Uri, true, null, def.Credentials); if (!string.IsNullOrWhiteSpace(bypassDefinition)) { proxy.BypassList = bypassDefinition.SmartSplit(false, true, ' ').ToArray(); } if (proxy.IsBypassed(uri)) { return(null); } return(proxy); }
public static void WebProxy_InvalidBypassUrl_AddedDirectlyToList_SilentlyEaten() { var p = new WebProxy("http://bing.com"); p.BypassArrayList.Add("*.com"); p.IsBypassed(new Uri("http://microsoft.com")); // exception should be silently eaten }
public void IsBypassed_ShouldReturnValuePassedToCtor(bool bypassOnLocal) { var uri = new Uri("http://proxy-example.com:9090"); var proxy = new WebProxy(uri, bypassOnLocal); Assert.True(proxy.IsBypassed(new Uri("https://airbrake.io/")) == bypassOnLocal); }
public static void WebProxy_InvalidArgs_Throws() { var p = new WebProxy(); AssertExtensions.Throws <ArgumentNullException>("destination", () => p.GetProxy(null)); AssertExtensions.Throws <ArgumentNullException>("host", () => p.IsBypassed(null)); Assert.ThrowsAny <ArgumentException>(() => p.BypassList = new string[] { "*.com" }); }
public void IsByPassed() { WebProxy p = new WebProxy("http://proxy.contoso.com", true); Assert.IsTrue(!p.IsBypassed(new Uri("http://www.google.com")), "#1"); Assert.IsTrue(p.IsBypassed(new Uri("http://localhost/index.html")), "#2"); Assert.IsTrue(p.IsBypassed(new Uri("http://localhost:8080/index.html")), "#3"); Assert.IsTrue(p.IsBypassed(new Uri("http://loopback:8080/index.html")), "#4"); Assert.IsTrue(p.IsBypassed(new Uri("http://127.0.0.01:8080/index.html")), "#5"); Assert.IsTrue(p.IsBypassed(new Uri("http://webserver/index.html")), "#6"); Assert.IsTrue(!p.IsBypassed(new Uri("http://webserver.com/index.html")), "#7"); p = new WebProxy("http://proxy.contoso.com", false); Assert.IsTrue(!p.IsBypassed(new Uri("http://www.google.com")), "#11"); Assert.IsTrue(p.IsBypassed(new Uri("http://localhost/index.html")), "#12: lamespec of ms.net"); Assert.IsTrue(p.IsBypassed(new Uri("http://localhost:8080/index.html")), "#13: lamespec of ms.net"); Assert.IsTrue(p.IsBypassed(new Uri("http://loopback:8080/index.html")), "#14: lamespec of ms.net"); Assert.IsTrue(p.IsBypassed(new Uri("http://127.0.0.01:8080/index.html")), "#15: lamespec of ms.net"); Assert.IsTrue(!p.IsBypassed(new Uri("http://webserver/index.html")), "#16"); p.BypassList = new string [] { "google.com", "contoso.com" }; Assert.IsTrue(p.IsBypassed(new Uri("http://www.google.com")), "#20"); Assert.IsTrue(p.IsBypassed(new Uri("http://www.GOOGLE.com")), "#21"); Assert.IsTrue(p.IsBypassed(new Uri("http://www.contoso.com:8080/foo/bar/index.html")), "#22"); Assert.IsTrue(!p.IsBypassed(new Uri("http://www.contoso2.com:8080/foo/bar/index.html")), "#23"); Assert.IsTrue(!p.IsBypassed(new Uri("http://www.foo.com:8080/contoso.com.html")), "#24"); p.BypassList = new string [] { "https" }; Assert.IsTrue(!p.IsBypassed(new Uri("http://www.google.com")), "#30"); Assert.IsTrue(p.IsBypassed(new Uri("https://www.google.com")), "#31"); }
public void IsByPassed_Address_Null() { WebProxy p = new WebProxy((Uri)null, false); Assert.IsTrue(p.IsBypassed(new Uri("http://www.google.com")), "#1"); p = new WebProxy((Uri)null, true); Assert.IsTrue(p.IsBypassed(new Uri("http://www.google.com")), "#2"); }
public bool IsBypassed(Uri host) { bool retVal = m_inner.IsBypassed(host); if (retVal) { Debug.WriteLine(string.Format("BYPASSING PROXY for '{0}'.", host.AbsoluteUri)); } return(retVal); }
public void IsByPassed_Host_Null() { WebProxy p = new WebProxy("http://proxy.contoso.com", true); try { p.IsBypassed(null); Assert.Fail("#A1"); #if NET_2_0 } catch (ArgumentNullException ex) { Assert.AreEqual(typeof(ArgumentNullException), ex.GetType(), "#A2"); Assert.IsNotNull(ex.Message, "#A3"); Assert.IsNotNull(ex.ParamName, "#A4"); Assert.AreEqual("host", ex.ParamName, "#A5"); Assert.IsNull(ex.InnerException, "#A6"); } #else } catch (NullReferenceException) {
public static string PostRequest(string url, WebHeaderCollection headers) { var responseFromServer = ""; ServicePointManager.Expect100Continue = false; WebProxy myProxy = new WebProxy(); myProxy.IsBypassed(new Uri(url)); var request = (HttpWebRequest)WebRequest.Create(url); request.Proxy = myProxy; request.Method = "POST"; request.ContentType = "text/plain; charset=UTF-8"; request.Referer = Parse.BET365_HOME + "/"; request.Headers.Add("Origin", Parse.BET365_HOME); request.UserAgent = Parse.USER_AGENT; request.Accept = "*/*"; request.AutomaticDecompression = DecompressionMethods.GZip; request.KeepAlive = true; request.ContentLength = 0; request.Headers.Add(headers); request.Timeout = 1500; using (var response = (HttpWebResponse)request.GetResponse()) { using (var dataStream = response.GetResponseStream()) { Debug.Assert(dataStream != null, "dataStream != null"); using (BufferedStream buffer = new BufferedStream(dataStream)) { using (StreamReader readerStream = new StreamReader(buffer)) { responseFromServer = readerStream.ReadToEnd(); readerStream.Close(); } buffer.Close(); } response.Close(); dataStream.Close(); } } return(responseFromServer); }
//</snippet11> //<snippet12> public static WebProxy CreateProxyAndCheckBypass(bool bypassLocal) { // Do not use the proxy server for Contoso.com URIs. string[] bypassList = new string[] { ";*.Contoso.com" }; WebProxy proxy = new WebProxy("http://contoso", bypassLocal, bypassList); // Test the bypass list. if (!proxy.IsBypassed(new Uri("http://www.Contoso.com"))) { Console.WriteLine("Bypass not working!"); return(null); } else { Console.WriteLine("Bypass is working."); return(proxy); } }
public void IsByPassed_Host_Null() { WebProxy p = new WebProxy("http://proxy.contoso.com", true); try { p.IsBypassed(null); Assert.Fail("#A1"); } catch (ArgumentNullException ex) { Assert.AreEqual(typeof(ArgumentNullException), ex.GetType(), "#A2"); Assert.IsNotNull(ex.Message, "#A3"); Assert.IsNotNull(ex.ParamName, "#A4"); Assert.AreEqual("host", ex.ParamName, "#A5"); Assert.IsNull(ex.InnerException, "#A6"); } p = new WebProxy((Uri)null); try { p.IsBypassed(null); Assert.Fail("#B1"); } catch (ArgumentNullException ex) { Assert.AreEqual(typeof(ArgumentNullException), ex.GetType(), "#B2"); Assert.IsNotNull(ex.Message, "#B3"); Assert.IsNotNull(ex.ParamName, "#B4"); Assert.AreEqual("host", ex.ParamName, "#B5"); Assert.IsNull(ex.InnerException, "#B6"); } p = new WebProxy((Uri)null, true); try { p.IsBypassed(null); Assert.Fail("#C1"); } catch (ArgumentNullException ex) { Assert.AreEqual(typeof(ArgumentNullException), ex.GetType(), "#C2"); Assert.IsNotNull(ex.Message, "#C3"); Assert.IsNotNull(ex.ParamName, "#C4"); Assert.AreEqual("host", ex.ParamName, "#C5"); Assert.IsNull(ex.InnerException, "#C6"); } }
/// <summary> /// Метод формирования системного прокси /// </summary> /// <param name="uri"></param> /// <param name="bypassDefinition"></param> /// <returns></returns> /// <exception cref="NotImplementedException"></exception> public static WebProxy SelectSystemProxy(Uri uri, string bypassDefinition) { if (WebRequest.DefaultWebProxy.IsBypassed(uri)) { return(null); } var proxy = new WebProxy( WebRequest.DefaultWebProxy.GetProxy(uri).ToString(), true, null, CredentialCache.DefaultCredentials); if (!string.IsNullOrWhiteSpace(bypassDefinition)) { proxy.BypassList = bypassDefinition.SmartSplit(false, true, ' ').ToArray(); } //second check - bypass list updated if (proxy.IsBypassed(uri)) { return(null); } return(proxy); }
public static string webString(string url) { string htmlString = string.Empty; try { using (WebClient webClient = new WebClient()) { try { webClient.Headers["User-Agent"] = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15"; WebProxy myProxy = new WebProxy(); myProxy.IsBypassed(new Uri(url)); webClient.Proxy = myProxy; htmlString = webClient.DownloadString(url); } catch (Exception ex) { throw; } finally { webClient.Dispose(); } } } catch (WebException wex) { throw; } catch (Exception ex) { throw; } finally { } return(htmlString.Replace("\r", string.Empty).Replace("\n", string.Empty).Replace("\t", string.Empty)); }
public bool IsBypassed(Uri host) { return(proxy.IsBypassed(host)); }
public static void WebProxy_BypassUrl_BypassArrayListChangedDirectly_IsBypassedAsExpected() { var p = new WebProxy("http://microsoft.com", BypassOnLocal: false); Assert.False(p.IsBypassed(new Uri("http://bing.com"))); p.BypassArrayList.Add("bing"); Assert.True(p.IsBypassed(new Uri("http://bing.com"))); p.BypassArrayList.Remove("bing"); Assert.False(p.IsBypassed(new Uri("http://bing.com"))); p.BypassArrayList.AddRange(new[] { "dot.net" }); Assert.True(p.IsBypassed(new Uri("http://dot.net"))); p.BypassArrayList.InsertRange(0, new[] { "bing" }); Assert.True(p.IsBypassed(new Uri("http://bing.com"))); p.BypassArrayList.SetRange(0, new[] { "example", "microsoft" }); Assert.True(p.IsBypassed(new Uri("http://example.com"))); Assert.True(p.IsBypassed(new Uri("http://microsoft.com"))); Assert.False(p.IsBypassed(new Uri("http://bing.com"))); Assert.False(p.IsBypassed(new Uri("http://dot.net"))); p.BypassArrayList.Clear(); Assert.False(p.IsBypassed(new Uri("http://example.com"))); Assert.False(p.IsBypassed(new Uri("http://microsoft.com"))); p.BypassArrayList.Insert(0, "bing"); p.BypassArrayList.Insert(1, "example"); Assert.True(p.IsBypassed(new Uri("http://bing.com"))); Assert.True(p.IsBypassed(new Uri("http://example.com"))); p.BypassArrayList.RemoveAt(0); Assert.False(p.IsBypassed(new Uri("http://bing.com"))); Assert.True(p.IsBypassed(new Uri("http://example.com"))); p.BypassArrayList.RemoveRange(0, 1); Assert.False(p.IsBypassed(new Uri("http://example.com"))); }
public static void InvalidBypassUrl_AddedDirectlyToList_SilentlyEaten() { var p = new WebProxy("http://bing.com"); p.BypassArrayList.Add("*.com"); p.IsBypassed(new Uri("http://microsoft.com")); // exception should be silently eaten }
public static void InvalidArgs_Throws() { var p = new WebProxy(); Assert.Throws<ArgumentNullException>("destination", () => p.GetProxy(null)); Assert.Throws<ArgumentNullException>("host", () => p.IsBypassed(null)); Assert.Throws<ArgumentNullException>("c", () => p.BypassList = null); Assert.Throws<ArgumentException>(() => p.BypassList = new string[] { "*.com" }); }