周末的時候,看到博客園里面有贈書活動,需要通過准時准點的回復,才能獲得。一看時間。我勒個去! 這都結束三天了。在看看其他博客,居然還有JS版本科學搶書工具。 我一想,不錯哦。 我也弄個WinForm版本搶書工具來,雖然搶不到書了,但是可以過下“外掛”癮。
程序是今天利用午休的40分鍾搞定的。沒有經過嚴謹的推敲,代碼也很簡單。原理:就是C#通過webbrowser控件調用博客園博客中的控件以及事件進行處理而已。
本程序也可以用到其他網頁登陸(如新浪,騰訊微薄,空間等等,只需要更新后台代碼中相應界面中的登陸控件信息即可。)
完整代碼如下:
/// <summary> /// 打開網址 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOpenWeb_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtWebAddress.Text)) { webbrWebPage.Navigate(txtWebAddress.Text.Trim()); } } /// <summary> ///登陸 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { HtmlElement name = GetElement_Id("tbUserName");//獲取webbrowser中用戶名和密碼登錄控件 HtmlElement pwd = GetElement_Id("tbPassword"); if (!string.IsNullOrEmpty(txtUserName.Text) && !string.IsNullOrEmpty(txtUserPwd.Text)) { name.SetAttribute("value", txtUserName.Text.Trim());//賦值 pwd.SetAttribute("value", txtUserPwd.Text.Trim()); Thread.Sleep(2000); //數據錄入成功后,登陸界面 if (GetElement_Type("submit") != null) GetElement_Type("submit").InvokeMember("click");//觸發登陸事件 } else { MessageBox.Show("Please Input the UserName and UserPassword!"); } } private void webbrWebPage_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { hdoc = webbrWebPage.Document; timer1.Enabled = false; } /// <summary> /// 根據name獲取元素 /// </summary> /// <param name="name"></param> /// <returns></returns> private HtmlElement GetElement_Name(string name) { HtmlElement e = hdoc.All[name]; return e; } private HtmlElement GetElement_Id(string id) { HtmlElement e = hdoc.GetElementById(id); return e; } private HtmlElement GetElement_Type(string type) { HtmlElement e = null; HtmlElementCollection elements = hdoc.GetElementsByTagName("input"); foreach (HtmlElement item in elements) { if (item.GetAttribute("type") == type) { e = item; } } return e; } /// <summary> /// 手動回復 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { if (webbrWebPage.Document.GetElementById("tbCommentBody")!=null) { webbrWebPage.Document.GetElementById("tbCommentBody").SetAttribute("value", txtanswer.Text);//界面上錄入需要說明的文字 } if (webbrWebPage.Document.GetElementById("btn_comment_submit") != null) { webbrWebPage.Document.GetElementById("btn_comment_submit").InvokeMember("click");//觸發提交事件 } } private void timer1_Tick(object sender, EventArgs e) { string strDateNow=string.Format("{0}:{1}",DateTime.Now.Hour,DateTime.Now.Minute);//后面也可以追加需要的毫秒 if (strDateNow.Equals("16:55"))//簡單判斷(時間有限,所以此處沒有考慮鄙人本機時間和博客園服務器時間的誤差) { button2_Click(sender, e);//調用button2(回復)的click事件 timer1.Enabled = false; } } /// <summary> /// 開啟timer /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_Click(object sender, EventArgs e) { timer1.Enabled = true; timer1.Interval = 1000;//1s }
代碼很簡單,而且代碼后面都添加有注釋,就不一一解釋了。 如果有任何疑問,歡迎留言,一起進步..
最后曬出程序運行結果(由於時間短,沒有在美化界面上花時間,大家就將就着看吧 ,不過為了自身安全,不要用於非法操作上,否則后果自負。)