首页 > 学院 > 开发设计 > 正文

【原创】验证代理IP是否有用

2019-11-17 01:43:56
字体:
来源:转载
供稿:网友

【原创】验证代理ip是否有用

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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表