【Talk is cheap. Show me the code.】 公众号如有回复不及时的,麻烦点击联系关于我-联系博主,微信我。谢谢!
老帅哥

Stephen-kzx的博客

【Talk is cheap. Show me the code.】【公众号如有回复不及时的,麻烦点击联系关于我-联系博主,微信我。谢谢!】

WinForm版抢书工具(可扩展..)

             周末的时候,看到博客园里面有赠书活动,需要通过准时准点的回复,才能获得。一看时间。我勒个去! 这都结束三天了。在看看其他博客,居然还有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
        }

           代码很简单,而且代码后面都添加有注释,就不一一解释了。 如果有任何疑问,欢迎留言,一起进步..

 

           最后晒出程序运行结果(由于时间短,没有在美化界面上花时间,大家就将就着看吧 ,不过为了自身安全,不要用于非法操作上,否则后果自负。)

          

posted @ 2012-10-29 17:21  何以解忧唯有撸码  阅读(5580)  评论(26编辑  收藏  举报