Exemplo n.º 1
0
        private void CheckCompleted(object obj, EventArgs arg)
        {
            if (obj.GetType().Name == "RefreshThread")
            {
                RefreshThread thread = obj as RefreshThread;
                if (thread.Status == "Completed")
                {
                    this.CompletedCount++;
                }
            }

            if (obj.GetType().Name == "WbRefreshThread")
            {
                WbRefreshThread thread = obj as WbRefreshThread;
                if (thread.Status == "Completed")
                {
                    this.CompletedCount++;
                }
            }

            if (this.CompletedCount == this.threads.Count)
            {
                this.Completed(this.Completed, new EventArgs()); //发出警告
            }
        }
Exemplo n.º 2
0
        public static string GetHtmlString(RefreshThread refresher, string proxyServer)
        {
            Stream    data = null;
            Stopwatch sw   = new Stopwatch();

            sw.Start();
            string result = "";

            try
            {
                string html = "";

                WebClient client = new WebClient();
                refresher.Description = proxyServer + "开始设置代理...";
                WebProxy proxy = WebProxy.GetDefaultProxy();
                proxy.Address         = new Uri("http://" + proxyServer + "/");
                client.Proxy          = proxy;
                refresher.Description = proxyServer + "设置完成,开刷...";
                data = client.OpenRead(refresher.url);


                using (StreamReader reader = new StreamReader(data, System.Text.Encoding.GetEncoding("GB2312")))
                {
                    result = reader.ReadLine();
                    while (result != null)
                    {
                        html  += result;
                        result = reader.ReadLine();
                    }
                }
                sw.Stop();
                refresher.Description = proxyServer + "搞定,耗时:" + sw.ElapsedMilliseconds;
                refresher.SuccessCount++;
                return(html);
            }
            catch (Exception ex)
            {
                sw.Stop();
                refresher.FailedCount++;
                refresher.Description = proxyServer + "有问题:" + ex.Message + sw.ElapsedMilliseconds;
                return(result);
            }
            finally
            {
                if (data != null)
                {
                    data.Close();
                }
            }
        }
Exemplo n.º 3
0
        private static void DoWork(object data)
        {
            RefreshThread refresher = (RefreshThread)data;

            try
            {
                List <Proxy> pList = refresher.proxyList;

                foreach (Proxy proxy in pList)
                {
                    refresher.Description = proxy.IpAndPort + "开始刷新...";
                    try
                    {
                        Refresh(refresher, proxy);
                        RefreshForm fm = (RefreshForm)Application.OpenForms["RefreshForm"];
                        if (null != fm)
                        {
                            fm.UpdateDataGrid();
                        }
                        Thread.Sleep(refresher.sleepTime * 1000);
                    }
                    catch (ThreadAbortException)
                    {
                    }
                    catch (Exception)
                    {
                    }
                    finally
                    {
                        refresher.TotalCount++;
                    }
                    refresher.Status = refresher.thread.ThreadState.ToString();
                }

                refresher.Status = "Completed";
            }
            catch (Exception ex)
            {
                // 线程被放弃
                refresher.Status = "Completed";
                Console.WriteLine(ex.Message);
                //WriteException(ex);
            }
            finally
            {
                refresher.OnCompleted();
            }
        }
Exemplo n.º 4
0
        public void Stop()
        {
            foreach (object thread in this.Threads)
            {
                if (thread.GetType().Name == "RefreshThread")
                {
                    RefreshThread t = (RefreshThread)thread;
                    t.Abort();
                }

                if (thread.GetType().Name == "WbRefreshThread")
                {
                    WbRefreshThread t = (WbRefreshThread)thread;
                    t.Abort();
                }
            }
            this.Completed(this.Completed, new EventArgs()); //发出警告
        }
Exemplo n.º 5
0
 public void Start()
 {
     foreach (string url in urlList)
     {
         if (this.refreshType == "Quickly")
         {
             RefreshThread thread = new RefreshThread(proxyList, url, this.SleepTime, this.TimeOut);
             thread.Completed += new RefreshThread.CompletedEventHandler(this.CheckCompleted);
             thread.Start();
             threads.Add(thread);
         }
         else
         {
             WbRefreshThread thread = new WbRefreshThread(proxyList, url, useProxy, this.TimeOut);
             thread.Completed += new WbRefreshThread.CompletedEventHandler(this.CheckCompleted);
             thread.Start();
             threads.Add(thread);
         }
     }
 }
Exemplo n.º 6
0
        public static string GetHtmlString(Proxy proxy, RefreshThread refresher)
        {
            string result = "";

            if (proxy == null)
            {
                return(result);
            }
            Stopwatch sw = new Stopwatch();

            sw.Start();


            Random r   = new Random();
            string url = refresher.url;
            //if (url.Contains("?"))
            //{
            //    url = url + "&p=" + r.Next(99999);
            //}
            //else
            //{
            //    url = url + "?p=" + r.Next(99999);
            //}

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url).ToString());

            try
            {
                WebProxy proxyServer = WebProxy.GetDefaultProxy();
                if (!string.IsNullOrEmpty(proxy.IP) && proxy.Port != 0)
                {
                    proxyServer.Address = new Uri("http://" + proxy.IpAndPort + "/");
                }
                webRequest.Proxy      = proxyServer;
                refresher.Description = proxy.IpAndPort + "设置完成,开始访问网页...";

                webRequest.Method    = "Get";
                webRequest.Accept    = "*/*";
                webRequest.UserAgent = "Mozilla/4.0 (MSIE 6.0; Windows NT " + r.Next(99999) + ")";
                webRequest.Headers["Accept-Language"] = "zh-cn";
                webRequest.ContentType                  = "application/x-www-form-urlencoded";
                webRequest.Referer                      = "http://www.loamen.com/404.htm?name=refresher&url=" + url;
                webRequest.Timeout                      = refresher.timeOut * 1000;
                webRequest.ReadWriteTimeout             = refresher.timeOut * 1000;
                webRequest.ServicePoint.ConnectionLimit = 100;

                WebResponse response = webRequest.GetResponse();
                result = GetHtmlString(response, Encoding.GetEncoding("GB2312"));
                response.Close();
                sw.Stop();
                refresher.Description = proxy.IpAndPort + "刷新完毕,耗时:" + sw.ElapsedMilliseconds;
                refresher.SuccessCount++;
                return(result);
            }
            catch (Exception ex)
            {
                sw.Stop();
                refresher.FailedCount++;
                refresher.Description = proxy.IpAndPort + "出错啦:" + ex.Message + sw.ElapsedMilliseconds;
                return(result);
            }
            finally
            {
                webRequest.Abort();
            }
        }
Exemplo n.º 7
0
 private static void Refresh(RefreshThread refresher, Proxy proxy)
 {
     GetHtmlString(proxy, refresher); //httpWebRequest方式
     //GetHtmlString(refresher,proxy.IpAndPort); //webClient方式
 }