Exemplo n.º 1
0
 bool yanzhen(string ipStr, int port)
 {
     try
     {
         HttpWebRequest  Req;
         HttpWebResponse Resp;
         WebProxy        proxyObject = new WebProxy(ipStr, port); // port为端口号 整数型
         Req         = WebRequest.Create("http://www.baidu.com/s?wd=ip&ie=utf-8&tn=94523140_hao_pg") as HttpWebRequest;
         Req.Proxy   = proxyObject;                               //设置代理
         Req.Timeout = 1000;                                      //超时
         Resp        = (HttpWebResponse)Req.GetResponse();
         Encoding bin = Encoding.GetEncoding("UTF-8");
         using (StreamReader sr = new StreamReader(Resp.GetResponseStream(), bin))
         {
             string str = sr.ReadToEnd();
             if (str.Contains(ipStr))
             {
                 Resp.Close();
                 return(true);
             }
             else
             {
                 return(false);
             }
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return(false);
     }
 }
Exemplo n.º 2
0
 private bool VerIP(string ip, int port)
 {
     try
     {
         HttpWebRequest  Req;
         HttpWebResponse Resp;
         WebProxy        proxyObject = new WebProxy(ip, port); // port为端口号 整数型
         Req         = WebRequest.Create("https://www.baidu.com") as HttpWebRequest;
         Req.Proxy   = proxyObject;                            //设置代理
         Req.Timeout = 1000;                                   //超时
         Resp        = (HttpWebResponse)Req.GetResponse();
         Encoding bin = Encoding.GetEncoding("UTF-8");
         using (StreamReader sr = new StreamReader(Resp.GetResponseStream(), bin))
         {
             string str = sr.ReadToEnd();
             if (str.Contains("百度"))
             {
                 Resp.Close();
                 return(true);
             }
             else
             {
                 return(false);
             }
         }
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// 通过登录访问    登录访问失败,账号请求过多被锁,访问403
 /// </summary>
 /// <param name="url">访问地址</param>
 /// <param name="html">得到的html字符串</param>
 static void getHtmlByCookie(string url, ref string html)
 {
     try
     {
         HttpWebRequest  Req;
         HttpWebResponse Resp;
         Req         = WebRequest.Create(url) as HttpWebRequest;
         Req.Timeout = 8000;   //超时
         Req.Headers.Add("Cookie", cookie);
         Req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36";
         Resp          = (HttpWebResponse)Req.GetResponse();
         Encoding bin = Encoding.GetEncoding("UTF-8");
         using (StreamReader sr = new StreamReader(Resp.GetResponseStream(), bin))
         {
             string str = sr.ReadToEnd();
             if (str.Contains("异常"))
             {
                 Console.WriteLine("");
             }
             else
             {
                 html = str;
             }
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
Exemplo n.º 4
0
        private List <string> checkIP(int s, int e)
        {
            List <string> ret = new List <string>();

            for (int i = s; i < e; i++)
            {
                string[]   sArr        = IP[i].Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                WebProxy   proxyObject = new WebProxy(sArr[0], Convert.ToInt16(sArr[1])); // port为端口号 整数型
                WebRequest Req         = WebRequest.Create("http://www.baidu.com") as HttpWebRequest;
                Req.Proxy   = proxyObject;                                                //设置代理
                Req.Timeout = 5000;                                                       //超时
                HttpWebResponse Resp;
                try
                {
                    Resp = (HttpWebResponse)Req.GetResponse();
                    Encoding     bin = Encoding.GetEncoding("UTF-8");
                    StreamReader sr  = new StreamReader(Resp.GetResponseStream(), bin);
                    string       str = sr.ReadToEnd();
                    if (str.Contains("百度"))
                    {
                        ret.Add(IP[i]);
                    }
                    sr.Close();
                    sr.Dispose();
                }
                catch
                {
                    continue;
                }
            }
            return(ret);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 验证list集合里面的代理IP
        /// </summary>
        /// <param name="msg"></param>
        public static void ProxyVerification(object msg, string name)
        {
            if (null == msg)
            {
                return;
            }
            ProxyViewModel proxy = (ProxyViewModel)msg;

            try
            {
                using (WebClient web = new WebClient())
                {
                    try
                    {
                        HttpWebRequest  Req;
                        HttpWebResponse Resp;
                        WebProxy        proxyObject = new WebProxy(proxy.ProxyIP, proxy.ProxyPort);
                        Req         = WebRequest.Create("https://www.baidu.com") as HttpWebRequest;
                        Req.Proxy   = proxyObject; //设置代理
                        Req.Timeout = 3000;        //超时
                        Resp        = (HttpWebResponse)Req.GetResponse();
                        Encoding bin = Encoding.GetEncoding("UTF-8");
                        using (StreamReader sr = new StreamReader(Resp.GetResponseStream(), bin))
                        {
                            string str = sr.ReadToEnd();
                            if (str.Contains("百度"))
                            {
                                Resp.Close();
                                // 更新验证时间
                                proxy.CreateTime = DateTime.Now;
                                // 更新验证状态
                                proxy.State = 1;
                                // 验证通过,归队
                                QueueOperation(proxy, IQueueType.EnQueue);
                                Console.WriteLine("{0}> [{2}]自动验证成功{1}", DateTime.Now.ToString("s"), proxy.Id, name);
                            }
                            else
                            {
                                Console.WriteLine("{0}> [{2}]自动验证失败{1}", DateTime.Now.ToString("s"), proxy.Id, name);
                            }
                        }
                    }
                    catch (Exception ex)
                    { }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("{0}> [{3}]自动验证异常{1} {2}", DateTime.Now.ToString("s"), proxy.Id, e.Message, name);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 通过代理ip访问地址
        /// </summary>
        /// <param name="url">要访问的地址</param>
        /// <param name="html">返回的html</param>
        static void getHtmlstr(string url, ref string html)
        {
            Console.WriteLine("当前代理ip数量" + _ProxyQueue.Where(model => model.State == 1).Count());
            var proxy = _ProxyQueue.FirstOrDefault(model => model.State == 1);//从代理iplist中取一个

            if (proxy == null)
            {
                XiciGaoni();
                getHtmlstr(url, ref html);
            }
            else
            {
                try
                {
                    Console.WriteLine("开始使用" + proxy.ProxyIP + ":" + proxy.ProxyPort + "访问 : " + url);
                    HttpWebRequest  Req;
                    HttpWebResponse Resp;
                    WebProxy        proxyObject = new WebProxy(proxy.ProxyIP, proxy.ProxyPort); // port为端口号 整数型
                    Req           = WebRequest.Create(url) as HttpWebRequest;
                    Req.Proxy     = proxyObject;                                                //设置代理
                    Req.Timeout   = 2000;                                                       //超时
                    Req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36";
                    Resp          = (HttpWebResponse)Req.GetResponse();
                    Encoding bin = Encoding.GetEncoding("UTF-8");
                    using (StreamReader sr = new StreamReader(Resp.GetResponseStream(), bin))
                    {
                        string str = sr.ReadToEnd();
                        if (str.Contains("异常"))
                        {
                            Resp.Close();
                            // 更新验证状态
                            proxy.State = 0;
                            getHtmlstr(url, ref html);
                        }
                        else
                        {
                            Console.WriteLine("访问成功!");
                            html = str;
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("使用" + proxy.ProxyIP + ":" + proxy.ProxyPort + " 发生 " + e.Message);
                    proxy.State = 0;
                    getHtmlstr(url, ref html);
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// 验证将要从接口出去的代理IP
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool DbVerIp(ProxyViewModel proxy)
        {
            bool      success = false;
            Stopwatch sw      = new Stopwatch();

            sw.Start();
            try
            {
                HttpWebRequest  Req;
                HttpWebResponse Resp;
                WebProxy        proxyObject = new WebProxy(proxy.ProxyIP, proxy.ProxyPort); // port为端口号 整数型
                Req         = WebRequest.Create("https://www.baidu.com") as HttpWebRequest;
                Req.Proxy   = proxyObject;                                                  //设置代理
                Req.Timeout = 3000;                                                         //超时
                Resp        = (HttpWebResponse)Req.GetResponse();
                Encoding bin = Encoding.GetEncoding("UTF-8");
                using (StreamReader sr = new StreamReader(Resp.GetResponseStream(), bin))
                {
                    string str = sr.ReadToEnd();
                    if (str.Contains("百度"))
                    {
                        Resp.Close();
                        // 更新验证时间
                        proxy.CreateTime = DateTime.Now;
                        // 更新验证状态
                        proxy.State = 1;
                        // 记录验证状态
                        success = true;
                        // 验证通过,归队
                        QueueOperation(proxy, IQueueType.EnQueue);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("{0}> 接口验证异常{1}", DateTime.Now.ToString("s"), e.Message);
            }
            sw.Stop();
            Console.WriteLine("{0}> 接口验证{1} {2} 耗时{3}s", DateTime.Now.ToString("s"), proxy.Id, success, sw.ElapsedMilliseconds / 1000.00);
            return(success);
        }