Http proxy test, http Proxy
Technorati flag: http Proxy verification and testing Technorati flag: C #
Based on a lot of information on the Internet, the final code is summarized as follows:
Using System; using System. collections; using System. collections. generic; using System. configuration; using System. diagnostics. contracts; using System. IO; using System. linq; using System. net; using System. net. networkInformation; using System. text; using System. threading. tasks; using static System. console; using static System. GC; using static System. net. webRequest; namespace proxy test {class Program {in T printCount; Uri uri; Encoding bin; string [] IpSectionSpan = {"10.197.255. "," 10.197.198. "}; NetworkCredential credit; [STAThread] static void Main (string [] args) {new Program (); ReadKey ();} public Program () {var dr = new AppSettingsReader (); credit = new NetworkCredential (dr. getValue ("uid", typeof (string )). toString (), dr. getValue ("pwd", typeof (string )). toString (); // var creadentiCache = n Ew CredentialCache (); uri = new Uri ("http://wwww.baidu.com/"); bin = Encoding. getEncoding ("UTF-8"); // test all IP TestIPSetions ();} private void TestIPSetions () {// for (int I = 1; I <255; I ++) // {TestInSpan (198. toString (); //} private void TestInSpan (string ipSect) {for (var I = 255; I> 0; I --) {var wproxy = new WebProxy (@ "10.137. "+ ipSect + ". "+ I, 3128); // uri = new Uri ("http: // 10.137.255. "+ I +": 3128/"); // creadentiCache. add (uri, "Basic", credit); // wproxy. credentials = creadentiCache; wproxy. credentials = credit; // Collect (); var req = Create (uri) as HttpWebRequest; req. preAuthenticate = true; req. timeout = 1000; // Timeout req. proxy = wproxy; req. keepAlive = false; try {WebResponse resp; req. beginGetResponse (ra =>{ try {using (resp = req. endGetResponse (ra )){ If (resp! = Null) {var sr = new StreamReader (resp. GetResponseStream (), bin); var str = sr. ReadToEnd (); if (! Str. Contains ("Baidu") return; WriteLine ("{0} \ t: {1} \ t verified! ", PrintCount ++, wproxy. address); sr. close (); sr. dispose () ;}} catch (Exception ex) {WriteLine ("{0} \ t: {1} \ t Verification Failed, cause of failure: \ t {2 }", printCount ++, wproxy. address, ex. message) ;}}, null) ;}catch (Exception e) {WriteLine (e. message );}}}}}