`
caozuiba
  • 浏览: 901920 次
文章分类
社区版块
存档分类
最新评论

VS2005中使用webBrowser控件实现自动登录Gmail邮件的实例

 
阅读更多
<iframe marginwidth="0" marginheight="0" src="http://218.16.120.35:65001/PC/Global/images/b.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>

这是一个小实现,可以实现自动登录Gmail邮箱。

使用VS2005 C#新建一个Windows应用程序,

在Form1窗体上添加一个webBrowser控件,

编写如下代码,就可以实现:

usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;

namespaceWindowsApplication1
{
publicpartialclassForm1:Form
{
publicForm1()
{
InitializeComponent();
}

privatevoidForm1_Load(objectsender,EventArgse)
{
webBrowser1.Url
=newUri("http://www.gmail.com");
}

privatevoidwebBrowser1_DocumentCompleted(objectsender,WebBrowserDocumentCompletedEventArgse)
{
HtmlElementtxtUserHtml
=webBrowser1.Document.All["Email"];
HtmlElementtxtPasswordHtml
=webBrowser1.Document.All["Passwd"];
HtmlElementbtnClickHtml
=webBrowser1.Document.All["signIn"];


txtUserHtml.SetAttribute(
"value","这里填写用户名");//输入用户名
txtPasswordHtml.SetAttribute("value","这里填写密码");//输入密码
btnClickHtml.InvokeMember("click");
}
}
}

这是一个Windows Form与WebBrows交互的小示例。其中涉及到了Web页面上的文本输入框的填写和单击命令按钮的响应。这也是网页表单的最常见形式,填写完信息后,单击命令按钮提交信息。

※关于表单的提交,的确还有另一种方法就是获取form元素而不是button,并用form元素的submit方法: HtmlElement formLogin = webBrowser.Document.Forms["loginForm"]; /
/…… formLogin.InvokeMember("submit");   
本文之所以没有推荐这种方法,是因为现在的网页,很多都在submit按钮上添加onclick事件,以对提交的内容做最基本的验证。如果直接使用form的submit方法,这些验证代码就得不到执行,有可能会引起错误。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics