基本信息
源码名称:C#扫代理IP
源码大小:0.30M
文件格式:.zip
开发语言:C#
更新时间:2015-01-06
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

C#扫描代理IP

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GetProxyIP
{
    public partial class MainForm : Form
    {
        bool isRun = true;
        int ipCount = 0;
        Thread m_Thread;
        HttpHelper help = new HttpHelper();
        HttpResult result = new HttpResult();
        public List<string> list = new List<string>();

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            UpdateState(OleDbHelp.GetIpCount().ToString());
            //启动线程
            m_Thread = new Thread(new ThreadStart(GetProxyIp));
            m_Thread.Start();
        }

        /// <summary>
        /// 获取代理ip
        /// </summary>
        private void GetProxyIp()
        {
            while (true)
            {
                if (!isRun)
                {
                    Thread.Sleep(1);
                }
                list.Clear();
                ipCount = 0;
                WriteMsg("\r\n=======开始获取代理IP=======\r\n");
                HttpItem item = new HttpItem();
                item.URL = "http://www.xici.net.co/nt/"; // nn=国内高匿 nt=国内普通 wn=国外高匿 wt=国外普通
                item.Referer = "http://www.xici.net.co/";
                item.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
                result = help.GetHtml(item);
                string html = result.Html;
                if (html.Contains("本次请求并未返回任何数据") || html.Length < 24)
                    return;

                int start = html.IndexOf("验证时间")   24;
                if (start <= 24)
                    return;
                int end = html.IndexOf("西刺免费代理") - start;
                html = html.Substring(start, end).Replace("\n", "");
                html = html.Replace("<tr class=\"odd\">", "#").Replace("<tr class=\"\">", "#").Replace("</tr>", "#").Replace("<td>", "|").Replace("</td>", "|");

                string[] trs = html.Split('#');
                List<t_porxyip> iplist = new List<t_porxyip>();
                List<Thread> runList = new List<Thread>();

                for (int i = 0; i < trs.Length; i  )
                {
                    string[] tds = trs[i].Split('|');
                    if (tds.Length < 9)
                        continue;
                    t_porxyip info = new t_porxyip();
                    try
                    {
                        if (tds[11].ToLower() != "https" ||
                            tds[3].Trim() == "" ||
                            tds[5].Trim() == "" ||
                            tds[7].Trim() == "")
                            continue;
                        info.f_ip = tds[3].Trim();
                        info.f_port = Convert.ToInt32(tds[5].Trim());
                        info.f_area = tds[7].Trim();

                        OleDbHelp.InsertProxyIP(info);
                    }
                    catch (Exception ex)
                    {
                        Global.G_Function.WriteError(ex.Message);
                    }
                }

                List<t_porxyip> porxy = OleDbHelp.GetProxyIP();
                for (int i = 0; i < porxy.Count; i  )
                {
                    //测试访问速度,一分钟以内都算有效
                    CallWithTimeout(IPTest, porxy[i], 60000);
                }
                WriteMsg("本次更新有效IP "   ipCount   " 条");
                WriteMsg("\r\n=======结束获取代理IP=======\r\n");

                UpdateState(OleDbHelp.GetIpCount().ToString());

                GC.Collect();
                Thread.Sleep(10000);
            }
        }

        private void IPTest(object obj)
        {
            t_porxyip info = (t_porxyip)obj;
            string str = info.f_ip   ":"   info.f_port;
            for (int i = str.Length; i < 21; i  )
            {
                str  = " ";
            }
            WriteMsg(str   "--> ");

            HttpItem item = new HttpItem();
            item.URL = "https://nid.naver.com/user2/join.nhn?m=init&lang=ko_KR";
            item.ProxyIp = info.f_ip   ":"   info.f_port;
            TimeSpan t1 = new TimeSpan(DateTime.Now.Ticks);
            HttpResult result = new HttpHelper().GetHtml(item);
            TimeSpan t2 = new TimeSpan(DateTime.Now.Ticks);
            TimeSpan t3 = t1.Subtract(t2).Duration();
            info.f_speed = Convert.ToInt32(t3.TotalMilliseconds);

            if (result.Html.Contains("이용약관") || result.Html.Contains("同意使用条款") || result.Html.Contains("Agree to Naver's") || result.Html.Contains("同意使用條款"))
            {
                if (OleDbHelp.UpdateIPSpeedAndTime(info))
                {
                    WriteMsg("可用");
                    ipCount  ;
                    return;
                }
            }
            OleDbHelp.DeleteProxyIP(info.f_ip);
            WriteMsg("无效");
        }
        private void CallWithTimeout(Action<object> action, object obj, int timeoutMilliseconds)
        {
            Thread threadToKill = null;
            Action wrappedAction = () =>
            {
                threadToKill = Thread.CurrentThread;
                action(obj);
            };

            IAsyncResult result = wrappedAction.BeginInvoke(null, null);
            if (result.AsyncWaitHandle.WaitOne(timeoutMilliseconds))
            {
                wrappedAction.EndInvoke(result);
            }
            else
            {
                OleDbHelp.DeleteProxyIP(((t_porxyip)obj).f_ip);
                WriteMsg("无效");
                threadToKill.Abort();
                return;
            }
        }

        private void WriteMsg(string msg)
        {
            if (txtMsg.InvokeRequired)
            {
                MyInvoke myInvoke = new MyInvoke(WriteMsg);
                this.Invoke(myInvoke, new object[] { msg });
            }
            else
            {
                list.Add(msg);
                StringBuilder sb = new StringBuilder();
                foreach (string str in list)
                {
                    if (str.Contains("-->"))
                        sb.Append(str);
                    else
                        sb.AppendLine(str).AppendLine();
                }
                txtMsg.Text = sb.ToString();
                txtMsg.SelectionStart = txtMsg.Text.Length - 1;
                txtMsg.ScrollToCaret();
            }
        }
        private delegate void MyInvoke(string msg);

        private void UpdateState(string txt)
        {
            if (txtMsg.InvokeRequired)
            {
                MyInvoke2 myInvoke = new MyInvoke2(UpdateState);
                this.Invoke(myInvoke, new object[] { txt });
            }
            else
            {
                tip.Text = txt;
            }
        }
        private delegate void MyInvoke2(string txt);

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            m_Thread.Abort();
            System.Diagnostics.Process.GetCurrentProcess().Kill();
        }

        private void Start_Click(object sender, EventArgs e)
        {
            isRun = true;
            SetButtonEnabled();
        }

        private void Stop_Click(object sender, EventArgs e)
        {
            isRun = false;
            SetButtonEnabled();
        }
        private void SetButtonEnabled()
        {
            Start.Enabled = !Start.Enabled;
            Stop.Enabled = !Stop.Enabled;
        }
    }
}