/// <summary> /// 验证代理IP是否有用 /// </summary> /// <param name="ip">IP地址</param> /// <param name="port">端口号</param> /// <returns>可用返回true</returns> static bool IsEnabled(string ip, int port) { try { HttpWebRequest Req = (HttpWebRequest)WebRequest.Create("http://www.whatismyip.com.tw/"); WebProxy proxyObject = new WebProxy(ip, port);//IP地址,端口号 Req.Proxy = proxyObject; //设置代理 Req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:31.0) Gecko/20100101 Firefox/31.0"; HttpWebResponse Resp = (HttpWebResponse)Req.GetResponse(); Encoding code = Encoding.GetEncoding("UTF-8"); using (StreamReader sr = new StreamReader(Resp.GetResponseStream(), code)) { if (sr != null) { string strHtml = sr.ReadToEnd(); MatchCollection mc = Regex.Matches(strHtml, "<h2>(?<text>.*?)</h2>", RegexOptions.IgnoreCase); if (mc.Count > 0) { GroupCollection gc = mc[0].Groups; if (ip == gc["text"].Value) { return true; } } } } } catch { return false; } return false; }
时间: 2024-10-05 13:30:31