在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C#扫代理IP

C#扫代理IP

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.30M
  • 下载次数:58
  • 浏览次数:1173
  • 发布时间:2015-01-06
  • 实例类别:C#语言基础
  • 发 布 人:loveings
  • 文件格式:.zip
  • 所需积分:2
 相关标签: IP 代理 C# c

实例介绍

【实例简介】

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

标签: IP 代理 C# c

实例下载地址

C#扫代理IP

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警