C#網頁刷票器(實現代理IP投票)

來源:互聯網
上載者:User

和@陳宇翔的修行錄忙了一個星期寫了個刷票器來交軟體測試課的作業

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("載入逾時");        }     }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.