http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 3400|回复: 2

[毕业生提问] 飞哥、高手们给添加个线程好不??

[复制链接]
发表于 2014-8-15 16:17:47 | 显示全部楼层 |阅读模式
1金钱
  public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox1.Text = @"f:\网站名.txt";
            textBox2.Text = @"f:\文件名.txt";
            Control.CheckForIllegalCrossThreadCalls = false;//不可在多线程中随便使用
        }
              public List<String> list_domins = new List<String>();//list是个集合,为String型。名字为list_domins,开始之前先初始化。
             public List<String> list_dirs = new List<String>();//把集合中的字符串放入到list_dirs中。
               public ServerInfo[] serverinfos = { };//定义数组可以用于确定有关在哪里发布表单的信息。该数组名字为serverinfos的空数组。
            //public List<ServerInfo> sucess_result = new List<ServerInfo>();
            public Thread th;//扫描线程
            public Thread c_th;//更新扫描数量线程
            public List<Thread> list_th = new List<Thread>();//http访问线程的集合,放这里方便控制
            public int maxThead = 5;//最大线程数,默认10个
            public int sleepTime = 0;//扫描休息时间,默认不休息
            public int timeout = 10000; //默认超时10秒            
            public int scanCount = 0;//目录扫描次数
            //public long usedtime = 0;//扫描完成域名数
              public String method = "HEAD";//默认HEAD扫描方式
              public String showCode = "403,200";//显示状态
            
                private void button1_Click(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;//不可在多线程中随便使用
                //kaishi();
            zhixing();//方法
           }
         public void zhixing()
        {
            //拼接字符
            string[] wangzhi = File.ReadAllLines(textBox1.Text.Trim());//读取txt
            string[] wenjianming = File.ReadAllLines(textBox2.Text.Trim());
            int shuliang = (int)wangzhi.Length * (int)wenjianming.Length;//计算需要循环多少次
            foreach (string wangzhi1 in wangzhi)//循环读取txt文件的内容
            {
                foreach (string wenjianming1 in wenjianming)
                {
                    string strurl1 = "http://" + wangzhi1 + "/" + wenjianming1;
                    ThreadPool.QueueUserWorkItem(new WaitCallback(GetGeneralContent), strurl1);//创建一个线程池,并执行auto方法,strurl1是auto方法的参数
                }

            }
        }
     public void GetGeneralContent(object strUrl)
        {
            string strurl = strUrl as string;
            string strMsg = string.Empty;
            try
            {

                WebRequest request = WebRequest.Create(strurl);//链接浏览器
                WebResponse response = request.GetResponse();//取链接返回内容
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
                strMsg = reader.ReadToEnd();

                if (strMsg.Contains("404 - Page Not Found"))//判断是否出现404
                {
                     richTextBox1.AppendText(strurl + " " + "[404]" + "\n");
                }
                else
                {
                    richTextBox1.AppendText(strurl + " " + "[200]" + "\n");
                }
                reader.Close();
                reader.Dispose();
                response.Close();
            }
            catch
            { }
       }
public static int getHttpCode(String header)
        {
            int code = 0;
            try
            {
                //查找状态码
                if (header != null && !"".Equals(header))
                {
                    code = int.Parse(header.Split(' ')[1]);
                }
            }
            catch (Exception e)
            {
            }
            return code;
        }

        public static int getLength(String header)
        {

            int s_index = -1;
            int s_endIndex = -1;
            int length = 0;
            try
            {
                if ((s_index = header.IndexOf("content-length:")) != -1)
                {
                    if ((s_endIndex = header.IndexOf("\r", s_index, header.Length - s_index)) != -1)
                    {
                        String slength = header.Substring(s_index + 15, s_endIndex - s_index - 15).Trim();
                        length = int.Parse(slength);
                    }

                }
            }
            catch (Exception e)
            {
            }
            return length;
        }
       public void scan(Object osvinfo)
        {
            ServerInfo svinfo = (ServerInfo)osvinfo;
            if (svinfo.up404)
            {
                if (svinfo.powerBy.IndexOf("waf") != -1)
                {
                    return;
                }
                String header = HttpTools.getHttpHeaderByTCP(this.method, svinfo.url, this.timeout, 2048);
                int code   =   getHttpCode(header);
                int length =   getLength(header);
               if (code == 302 || code == 200)
                {
                    if (svinfo.code == code && length == svinfo.length)
                    {
                        svinfo.code = 404;
                    }
                    else if (this.method.Equals("GET") && !svinfo.html_404.Equals(""))
                    {
                        svinfo.code = 404;
                    }
                }
               // else if (Tools.have404str(header))
                //{
                //    svinfo.code = 404;
                //}
                else { svinfo.code = code; }
              }
            //else if ((code == 403 && length > 0))
            //{ svinfo.code = 404; }
            else
            {
               // svinfo.code = Tools.getHttpCode(HttpTools.getHttpHeaderByTCP(this.method, svinfo.url, this.timeout, 15));
          }
            if (svinfo.isdir == 0)
            {
                this.showCode = this.showCode.Replace("403", "").Replace("301", "");
            }
            if (this.showCode.IndexOf(svinfo.code + "") != -1 && svinfo.code != 0)
            {
               // this.Invoke(new DelegateAddItemToListView(), svinfo);
            }
                scanCount++;       }
       //指纹IP扫描
        public void zwScan(Object osvinfo)
        {
            ServerInfo svinfo = (ServerInfo)osvinfo;
            if (svinfo.up404)
            {
                svinfo.url += "/aabcdsgsgshsft/page404404.php.xxx";
            }
            String header = HttpTools.getHttpHeaderByTCP(this.method, svinfo.url, this.timeout, 600);
            //svinfo.html_404 = get404code(header);//方便页面不反回状态码时,智能404判断
            svinfo.code =getHttpCode(header);
            if (svinfo.up404)
            {
                svinfo.length = getLength(header);
            }
            if (svinfo.upzw)
            {
                svinfo.server = (header);
                svinfo.powerBy = (header);
            }
            if (svinfo.upip)
            {
                svinfo.ip = (svinfo.url);
            }
        if (svinfo.code != 0 && (svinfo.upip | svinfo.upzw))
            {
                this.Invoke(new DelegateAddItemToListView(AddItemToListView), svinfo);
            }
          if (svinfo.code == 0)
            {
                //响应为0,超时域名记录到日志
                FileTool.AppendLogToFile("logs/failed_domains.txt", svinfo.url);
            }
            else
            { //404信息按顺序放到集合
                serverinfos[svinfo.domainid] = svinfo;  }
           //扫描成功数加一
            scanCount++;
        }
      public void clearThread()
        {
            for (int i = 0; i < list_th.Count; i++)
            {
                if (list_th.Count <= 0) break;
                Thread cth = list_th;

                if (cth.IsAlive == false)
                {
                    list_th.Remove(cth);
                }
                Thread.Sleep(this.sleepTime);
            }
        }
       public void initThread(ServerInfo server, int type)
        {
            while (true)
            {
                clearThread();
                if (list_th.Count < this.maxThead)
                {

                    Thread th = null;
                    if (type == 1)
                    {
                        //th = new Thread(new ParameterizedThreadStart(Scan));
                    }
                    else
                    {
                        th = new Thread(new ParameterizedThreadStart(scan));
                    }
                    list_th.Add(th);
                    th.Start(server);
                    break;
                }
            }
        }
        delegate void DelegateAddItemToListView(ServerInfo svinfo);
        public void AddItemToListView(ServerInfo svinfo)
        {
            ListViewItem lvi = new ListViewItem(svinfo.id + "");
            lvi.SubItems.Add(svinfo.url);
            lvi.SubItems.Add(svinfo.code + "");
            lvi.SubItems.Add(svinfo.server + "");
            lvi.SubItems.Add(svinfo.powerBy + "");
            lvi.SubItems.Add(svinfo.ip + "");
            lvi.SubItems.Add(svinfo.type + "");
           // this.richTextBox1.Items.Add(lvi);
        }
    private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
        }      
    }
}
我的界面,2个textBox,2个button,1个lable,1个richTextBox1,1个ComboBox.ComboBox名字cmb_threadCount。 真心不会整了 求高手们帮忙。



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2014-8-15 16:27:33 | 显示全部楼层
那是因为你没有看懂线程是怎么用的, 我写的有文章

http://www.sufeinet.com/thread-3556-1-1.html

很简单只有你看懂这个例子,肯定能加上,与其让我们花心思看你的程序,不如你自己看看我的例子,不光能解决问题还能让自己多学点东西
回复

使用道具 举报

发表于 2014-8-16 11:01:33 | 显示全部楼层
大黑客呀
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-5 11:16

© 2014-2021

快速回复 返回顶部 返回列表