ccav的“我要上春晚”設定了投票功能,選人氣王。寫了一個簡單的刷票器。
先下班了。代碼有空再寫。
接昨天的。
比如這個投票頁面:http://cctv.cntv.cn/woyaoshangchunwan/vote/02/index.shtml
先隨便投一下,抓包。post的資料包如:
encoding=gb2312&_charset_=UTF-8&retUrl=http%3A%2F%2Fapp1.vote.cntv.cn%2FviewResult.jsp%3FvoteId%3D3434&formhash=69256b23&voteId=3434&type=html&items_6351=36451&63=%E7%8E%8B%E5%B0%8F%E4%BA%8C&65=社會安全號碼&submitBtn=%E6%8A%95%E7%A5%A8
提交後的處理頁面為 http://app2.vote.cntv.cn/makeVoteAction.do
一共提交了節目ID、編碼後的姓名、社會安全號碼幾項資訊。然後用HttpWebRequest類比發送資料即可。
社會安全號碼可以隨機產生,我是從一個excel表裡讀取的。
主要代碼如下:
/// <summary><br /> /// Handles the Click event of the btnVote control.<br /> /// </summary><br /> /// <param name="sender">The source of the event.</param><br /> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param><br /> private void btnVote_Click(object sender, EventArgs e)<br /> {<br /> try<br /> {<br /> int interval = Convert.ToInt32(this.txtInterval.Text.Trim());<br /> //讀取excel表格中的資料<br /> string FilePath = this.txtFileName.Text.Trim();<br /> OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FilePath + ";Extended Properties = Excel 8.0");<br /> conn.Open();<br /> string strSQL = "select * from [職工表$] ";<br /> OleDbCommand cmd = new OleDbCommand(strSQL, conn);<br /> OleDbDataReader odr = cmd.ExecuteReader();<br /> int i = 0;<br /> while (odr.Read())<br /> {<br /> //建立HttpWebRequest<br /> HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(this.txtAction.Text);</p><p> string postData = @"encoding=gb2312&_charset_=utf-8&retUrl=http%3A%2F%2Fapp1.vote.cntv.cn%2FviewResult.jsp%3FvoteId%3D3422&formhash=69256b23&voteId=3422&type=html&場次ID=節目ID&10=" + Uri.EscapeDataString(odr["姓名"].ToString()) + "&11=" + odr["社會安全號碼"].ToString() + "&submitBtn=%E6%8A%95%E7%A5%A8";</p><p> //在發送的內容裡包括中文和空格</p><p> ASCIIEncoding encoding = new ASCIIEncoding();<br /> byte[] byte1 = encoding.GetBytes(postData);//最終編碼後要上傳的資料<br /> //Setthecontenttypeofthedatabeingposted.<br /> myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";<br /> myHttpWebRequest.Method = "post";//post上傳方式<br /> //Setthecontentlengthofthestringbeingposted.<br /> //myHttpWebRequest.Referer = @"http://cctv.cntv.cn/woyaoshangchunwan/vote/02/index.shtml";<br /> myHttpWebRequest.ContentLength = byte1.Length;</p><p> Stream newStream = myHttpWebRequest.GetRequestStream();<br /> newStream.Write(byte1, 0, byte1.Length);<br /> //加上這句釋放資源 否則執行兩次就卡住了<br /> myHttpWebRequest.Abort();<br /> newStream.Close();</p><p> //Get the new response<br /> //HttpWebResponse response = myHttpWebRequest.GetResponse() as HttpWebResponse;<br /> //Stream responseStream = response.GetResponseStream();<br /> //StreamReader reader = new System.IO.StreamReader(responseStream);<br /> //string srcString = reader.ReadToEnd();<br /> //this.txtResponseInfo.Text = srcString;</p><p> Thread.Sleep(interval);<br /> i++;<br /> }<br /> this.txtResponseInfo.Text = "已處理完。共投票" + i.ToString() + "次";<br /> odr.Close();<br /> cmd.Dispose();<br /> conn.Close();<br /> conn.Dispose();</p><p> }<br /> catch(Exception ex)<br /> {<br /> MessageBox.Show("錯誤資訊:" + ex.Message);<br /> }</p><p> }
注意發送資料如果有中文的話要Uri.EscapeDataString()一下,這樣就基本可以運行了。繼續完善的話,可以加上設定投票節目ID、增加線程處理等。
這個投票沒有驗證碼,刷票的挺多的。