[转载]【WINFORM】C#中不通过浏览页面直接登入博客园的实现 – 悠游人生 – 博客园.
在CSDN上看到了这个例子,觉得蛮好玩的,于是拿来改了一下。
这个例子有点像“三人行采集器”中的多用户自动登录功能或者是像许多浏览器中自动填表(填充账号、密码)功能。
即不通过浏览网页,直接由客户端程序来执行一些页面上的操作(如点击登入按钮,登入系统等)。
登入博客园:
1 private void wbCnblog_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
2 {
3 wbCnblog.ScriptErrorsSuppressed = true;
4
5 if ((e.Url.ToString().ToLower().Equals(“http://passport.cnblogs.com/login.aspx“))) //博客园登陆页面
6 {
7 HtmlElement textboxUserId = this.wbCnblog.Document.GetElementById(“tbUserName“);
8 //如果没有ID,用 Name 获取
9 //HtmlElement textboxUserId = this.webBrowser1.Document.All[“登录用户名文本框的Name”];
10
11 HtmlElement textboxPassword = this.wbCnblog.Document.GetElementById(“tbPassword“);
12 //如果没有ID, 用Name 获取
13 //HtmlElement textboxPassword = this.webBrowser1.Document.All[“登录密码框的Name”];
14
15 HtmlElement buttonSubmit = this.wbCnblog.Document.GetElementById(“btnLogin“);
16 //如果没有ID, 用Name获取
17 //HtmlElement buttonSubmit = this.webBrowser1.Document.All[“登录按钮的Name”];
18
19 for (int i = this.listView_usr.SelectedItems.Count – 1; i >= 0; i–)
20 {
21 ListViewItem item = this.listView_usr.SelectedItems[i];
22 //this.listView_usr.Items.Remove(item);
23 string strUsr = item.SubItems[0].Text;
24 string strPwd = item.SubItems[1].Text;
25 textboxUserId.InnerText = strUsr; //填写帐号
26 textboxPassword.InnerText = strPwd; //填写密码
27 }
28
29
30 buttonSubmit.InvokeMember(“click“); //触发提交按钮的点击事件登录博客园
31 }
32 }
2 {
3 wbCnblog.ScriptErrorsSuppressed = true;
4
5 if ((e.Url.ToString().ToLower().Equals(“http://passport.cnblogs.com/login.aspx“))) //博客园登陆页面
6 {
7 HtmlElement textboxUserId = this.wbCnblog.Document.GetElementById(“tbUserName“);
8 //如果没有ID,用 Name 获取
9 //HtmlElement textboxUserId = this.webBrowser1.Document.All[“登录用户名文本框的Name”];
10
11 HtmlElement textboxPassword = this.wbCnblog.Document.GetElementById(“tbPassword“);
12 //如果没有ID, 用Name 获取
13 //HtmlElement textboxPassword = this.webBrowser1.Document.All[“登录密码框的Name”];
14
15 HtmlElement buttonSubmit = this.wbCnblog.Document.GetElementById(“btnLogin“);
16 //如果没有ID, 用Name获取
17 //HtmlElement buttonSubmit = this.webBrowser1.Document.All[“登录按钮的Name”];
18
19 for (int i = this.listView_usr.SelectedItems.Count – 1; i >= 0; i–)
20 {
21 ListViewItem item = this.listView_usr.SelectedItems[i];
22 //this.listView_usr.Items.Remove(item);
23 string strUsr = item.SubItems[0].Text;
24 string strPwd = item.SubItems[1].Text;
25 textboxUserId.InnerText = strUsr; //填写帐号
26 textboxPassword.InnerText = strPwd; //填写密码
27 }
28
29
30 buttonSubmit.InvokeMember(“click“); //触发提交按钮的点击事件登录博客园
31 }
32 }
登出博客园:
1 private void webBrowserQuit()
2 {
3 if (null == wbCnblog.Document)
4 {
5 return;
6 }
7
8 wbCnblog.Document.Cookie = “”;
9
10 HtmlElementCollection links = wbCnblog.Document.Links;
11 if (null == links)
12 {
13 return;
14 }
15
16 foreach (HtmlElement link in links)
17 {
18 if (null == link.InnerText)
19 {
20 continue;
21 }
22
23 if (link.InnerText.Equals(“退出“))
24 {
25 link.InvokeMember(“click“); //激发链接的点击事件
26 break;
27 }
28 }
29 }
2 {
3 if (null == wbCnblog.Document)
4 {
5 return;
6 }
7
8 wbCnblog.Document.Cookie = “”;
9
10 HtmlElementCollection links = wbCnblog.Document.Links;
11 if (null == links)
12 {
13 return;
14 }
15
16 foreach (HtmlElement link in links)
17 {
18 if (null == link.InnerText)
19 {
20 continue;
21 }
22
23 if (link.InnerText.Equals(“退出“))
24 {
25 link.InvokeMember(“click“); //激发链接的点击事件
26 break;
27 }
28 }
29 }
代码: cnblogLogin.rar
刚才粗心大意,发图的时候竟然将自己密码给暴露出来了,无奈只能重新修改密码了