现在的位置: 首页 > 综合 > 正文

C#网页刷票器(实现代理IP投票)

2013年09月17日 ⁄ 综合 ⁄ 共 7677字 ⁄ 字号 评论关闭

和@陈宇翔的修行录忙了一个星期写了个刷票器来交软件测试课的作业

PPT不是很好传,放到资源里面去,PPT地址:

源码地址:http://download.csdn.net/detail/a8887396/5344103

投票效果:

刷票地址: (有效期半年)

http://mwangbobo.jingdianet.com/Default.aspx

主要代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using mshtml;
using System.Net;
using System.Runtime.InteropServices;

namespace 刷票
{
    public partial class Form1 : Form
    {

        [DllImport(@"wininet",
    SetLastError = true,
    CharSet = CharSet.Auto,
    EntryPoint = "InternetSetOption",
    CallingConvention = CallingConvention.StdCall)]

        //即时刷新IE设置
        public static extern bool InternetSetOption(
            int hInternet,
            int dmOption,
            IntPtr lpBuffer,
            int dwBufferLength
        );

        //是否可以连接到internet
        [DllImport("wininet.dll")] 
        public extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
        //返回false就是能连接到internet
        public static bool IsConnectedToInternet() 
        {
            int Desc; return InternetGetConnectedState(out Desc, 0); 
        }




        int count_all = 0; //投票次数
        bool is_start = false; //开始标志
        bool is_interval = false; //是否使用时间间隔
        bool is_proxy = false;
      //  bool is_ip_changed = false;
        string url_str = "http://mwangbobo.jingdianet.com/Default.aspx";  //投票的指定网址
       // string url_str = "http://localhost:4128/WebSite1/Default.aspx";
        string file_path; //代理IP的文本路径
        string[] ip = new string[1024]; //代理IP
     //   string[] port = new string[1024]; //代理IP的端口
        int count_ip = 0; //代理IP个数
        

        public Form1()
        {
            InitializeComponent();
            webBrowser1.Navigate(url_str); //跳转到页面

        }


        /* ==================            投票模块                    ================== */
       

        //点击开始按钮
        private void btn_start_Click(object sender, EventArgs e)
        {
            if (!IsConnectedToInternet()) //检查网卡状态 是否连接到internet 如你把网线拔了就会出错, 不包括设置代理IP错误导致不能开网页的情况
            {
                MessageBox.Show("不能连接到internet");
                return;
            }

       //     if (is_start || webBrowser1.Url == null || webBrowser1.Url.ToString() != url_str) //开始之后不能再点开始无效 或者 当前页面不是投票页面无效
            {
       //         return;
            }
            is_start = true;
            
            int interval = 0;
            bool i = int.TryParse(text_interval.Text, out interval);//获取刷新间隔
            if (!i || interval == 0)  
            {
               //获取间隔失败 或者间隔为0 不使用计时器
                is_interval = false;
                complete_timer.Interval = 3000; //网页加载计时器 防止网页出现无法载入的情况。
            }
            else
            {
                vote_timer.Interval = interval * 1000; //因为是微秒 
                is_interval = true;
                complete_timer.Interval = interval * 1000 + 3000; //网页加载计时器 
            }


          

            if (check_ip.Checked)
            {
                is_proxy = true;
            }

       
            if (int.TryParse(text_count.Text.ToString(), out count_all)) //如果输入的是有效数字
            {

                if (count_all > 0) //刷票次数大于0
                {
                    if (is_proxy) count_all++;
                    //第一次投票不管是不是代理IP都不使用代理IP 所以加一次

                  
                    text_remain.Text = Convert.ToString(count_all);
                   // complete_timer.Enabled = true;//开始加载完成计时

                    if (is_interval)
                    {
                        vote_timer.Enabled = true; //启动计时器
                    }
                    else
                    {
                        vote();                 //不使用计时器时 直接投票
                    }  
                   
                }
            }
            else
            {
                is_start = false;
                is_interval = false;
                is_proxy = false;
                vote_timer.Enabled = false;
                vote_timer.Enabled = false;
            }
            
        }

        //计时器事件 进行投票
        private void vote_timer_Tick(object sender, EventArgs e)
        {
            if (!is_start || count_all == 0) return;
            vote_timer.Enabled = false;    
            vote();
        }




       //姓名 号码
        string[] xing = { "赵"," 钱", "孙", "李", "周", "吴", "郑", "王","冯", "陈", "楮", "卫", "蒋", "沈", "韩", "杨",
                         "朱", "秦", "尤", "许", "何", "吕", "施", "张","孔", "曹", "严", "华", "金", "魏", "陶", "姜",
                         "戚", "谢", "邹", "喻", "柏", "水", "窦", "章","云", "苏", "潘", "葛", "奚", "范", "彭", "郎"};

        string[] ming = { "昌睿", "泉龙", "建利", "泰福", "长财", "红良", "昌逊", "卓臻", "昌哲", "承哲", "卓逊", "武隆",
                        "冰姿", "斌", "书琪", "季方", "元峰", "美茹", "雪莉", "娜芮", "茜茹", "沛秀", "敬瑶", "依洁", "巧梅",
                       "卿青", "政光", "思远", "卿清", "志昆", "锡俊", "婉柔", "文雅", "俊刚", "祝赢", "蕾", "禧", "齐", "兮" };

        string[] haomaf = { "138", "139", "150" };


        //投票: 选checkbox 填表格 点提交
        private void vote()
        {
            //如果已经刷票次数用完了  初始化这些值 然后停止
            if (count_all <= 0)
            {
                //is_start = false;
                return;
            }

   
           text_remain.Text = Convert.ToString(--count_all); //剩余次数-1 并显示到剩余次数文本上

           if (is_proxy && count_all > 0)
           {
               setProxy(ip[count_all -1]);
           }


            Random r = new Random(); 
            string q = xing[r.Next(48)]; //随机选择的姓氏
            string k = ming[r.Next(39)]; //随机选择的名字
            Application.DoEvents(); 

            string haoma = haomaf[r.Next(3)];//随机生成号码头三位
            for (int i = 0; i < 8; i++) //号码后8位生成
            {
                haoma += r.Next(10);
            }

            complete_timer.Enabled = true; //开始网页加载计时

            
            try
            {


                //下面的代码有一定几率抛出异常  即在网页加载不到所需要的元素的时候.  
                // 异常时  直接结束 但在上面网页加载计时已经开始 

                //选择第二个选项 动作click
                webBrowser1.Document.GetElementById("MainContent_RadioButtonList1_2").InvokeMember("click");
                // Application.DoEvents();
                //填表 随机的名字
                webBrowser1.Document.GetElementById("MainContent_UserName").InnerText = q + k;
                // Application.DoEvents();
                //填表 随机的号码
                webBrowser1.Document.GetElementById("MainContent_UserPhone").InnerText = haoma;
                // Application.DoEvents();

                //选择提交按钮 动作click
                webBrowser1.Document.GetElementById("MainContent_OKButton").InvokeMember("click");
                Application.DoEvents();
            }
            catch (System.Exception ex)
            {
               // is_start = false;
               
                //MessageBox.Show("页面中未找到投票对象","错误");
                
            }

        }


        //网页加载成功阶段 webBrowser1_DocumentCompleted 此时判断是否处于刷票中,是则再投一次。
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            complete_timer.Enabled = false; //kai
            if (!is_start) return;
            if (count_all > 0) //继续投票
            {
                if (is_interval)
                {
                    //如果使用间隔计时器 那开始间隔计时
                    vote_timer.Enabled = true;
                }
                else
                {
                    //没有使用间隔计时器 直接开始投票
                    vote();
              
                }
            }
            else //投完票了,
            {
                is_start = false; //投票标志清0
                is_interval = false;
                if (is_proxy)
                {
                    disProxy();
                    is_proxy = false;
                }         
                vote_timer.Enabled = false; //不管有没有计时器 计时器标志清0
                complete_timer.Enabled = false;
                MessageBox.Show("投票完成");
                
            }
        }


        //WebBrowser Navigated阶段 :  控制项已巡览至新文件且已开始载入新文件时  另外还有Navigating 表示寻觅开始前
        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            IHTMLWindow2 win = (IHTMLWindow2)webBrowser1.Document.Window.DomWindow;
            string s = @"function confirm() {";
            s += @"return true;";
            s += @"}";
            s += @"function alert() {}";
            win.execScript(s, "javascript");

        }


        //结束按钮
        private void btn_end_Click(object sender, EventArgs e)
        {
            is_start = false;
            is_interval = false;
            is_proxy = false;
             disProxy();
            vote_timer.Enabled = false;
            MessageBox.Show("投票终止");

        }





        /* ================            代理IP模块              ====================
            1 导入代理IP
         *  2 使用代理IP
        */

        //导入代理IP的按钮 文本格式必须是IP:PORT才可以正确读取
        private void btn_ip_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog1 = new OpenFileDialog(); //文件选择框
            fileDialog1.InitialDirectory = "d://";       // 默认打开的路径,可更改
            fileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            fileDialog1.FilterIndex = 1;
            fileDialog1.RestoreDirectory = true;
            if (fileDialog1.ShowDialog() == DialogResult.OK)//点击OK选择文件
            {
                file_path = fileDialog1.FileName;
            }
            else
            {
                // file_path = null;
                return;
            }

            //  打开文件 获取IP与端口
            if (file_path.Length != 0)
            {

                FileStream fs = new FileStream(file_path, FileMode.Open);  //打开文件
                StreamReader m_streamReader = new StreamReader(fs); //读取文件
                m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin); //定位文件读取位置
                string strLine = m_streamReader.ReadLine(); //读取第一行
                int i = 0;
                do
                { 
                    ip[i] = strLine.Split('@')[0];
                   // textBox1.Text += ip[i];
                    strLine = m_streamReader.ReadLine();//读取下一行
                    i++;
                } while (strLine != null && strLine != "");
                m_streamReader.Close(); //关闭读取器
                m_streamReader.Dispose(); //释放资源
                fs.Close(); //关闭文件流
                fs.Dispose();//释放资源
                count_ip = i; //记录代理IP个数



           }
       }

        //点击代理IP的checkbox ,如果是使用代理IP,则将代理IP个数写到 刷票次数中 
        private void check_ip_CheckedChanged(object sender, EventArgs e)
        {
            if (check_ip.Checked == true)
            {
                text_count.Text = Convert.ToString(count_ip);
                text_count.Enabled = false;
            }
            else
            {
                text_count.Enabled = true;
            }
        }


        //设置代理IP
        private void setProxy(string ip)
        {
            if (ip == null || ip == "") return; //如果ip是空的 返回

            try
            {
       
                 //获取注册表对象
                Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);
                //设置代理可用 
                rk.SetValue("ProxyEnable", 1);
                //设置代理IP和端口 
                rk.SetValue("ProxyServer", ip);
                rk.Flush();
                rk.Close();
                //使立即生效
                InternetSetOption(0, 39, IntPtr.Zero, 0);
                InternetSetOption(0, 37, IntPtr.Zero, 0);

               // InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, NULL);
               // InternetSetOption(NULL, INTERNET_OPTION_REFRESH, NULL, NULL);

               // is_ip_changed = true;
                //webBrowser1.Refresh();
               // webBrowser1.Document.ExecCommand("Refresh", false, null);

            }
            catch (System.Exception ex)
            {

            }


        

        }

        private void disProxy()
        {
            try
            {
                Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);
                //设置代理可用 
                rk.SetValue("ProxyEnable", 0);
                //设置代理IP和端口 
                rk.SetValue("ProxyServer", "");
                rk.Flush();
                rk.Close();
            //    is_ip_changed = false;
                //使立即生效
                InternetSetOption(0, 39, IntPtr.Zero, 0);
                InternetSetOption(0, 37, IntPtr.Zero, 0);
            }
            catch (System.Exception ex)
            {
            	
            }

        }

        private void complete_timer_Tick(object sender, EventArgs e)
        {
            //count_all--;
            complete_timer.Enabled = false;
            disProxy();
            webBrowser1.Navigate(url_str);
            //webBrowser1_DocumentCompleted(null, null);
            //MessageBox.Show("加载超时");
        } 
    }
}

【上篇】
【下篇】

抱歉!评论已关闭.