c# 快速验证代理IP是否有用

时间:2022-11-14 15:30:01

 public void yanzhen(string str, int port)
        {
            WebProxy proxyObject = new WebProxy(str, port);//str为IP地址 port为端口号
            HttpWebRequest Req = (HttpWebRequest)WebRequest.Create("http://www.whatismyip.com.tw/");
            Req.Proxy = proxyObject; //设置代理
            HttpWebResponse Resp = (HttpWebResponse)Req.GetResponse();
            string str = "";
            string OkStr = "";
            Encoding code = Encoding.GetEncoding("UTF-8");
            using (StreamReader sr = new StreamReader(Resp.GetResponseStream(), code))
            {
                if (sr != null)
                {
                    try
                    {
                        str = sr.ReadToEnd();
                        StringSub.substr(str, "<h2>", "</h2>", 0);
                        str = str.Substring(str.IndexOf(start) + start.Length);
                        OkStr = str.Substring(0, str.IndexOf(last));
                        MessageBox.Show("验证成功!显示IP为" + OkStr);
                    }
                    catch
                    {
                        MessageBox.Show("文件读取失败!");
                    }
                    finally
                    {
                        sr.Close();
                    }
                }
            }
        }