C# 自动填表 & 关于WebBrowser submit

来源:互联网 发布:知乎的赵老爷 编辑:程序博客网 时间:2024/04/29 19:02

公司上网首先需要安全认证,我嫌每次输入用户名、密码太麻烦,而我又使用IE7、Maxthon、firefox2等多种浏览器,所以决定自己写个自动填表的小程序让它开机运行。
用C#写这个东东非常简单,只需要用一个WebBrowser控件打开要自动填表的网页,然后找到特定的页面成员,赋值和触发事件就ok了。
首先分析目标页面:
《form name="LoginForm" method="get" action="http://10.245.×.×/login" onsubmit="'return">
《table width="253" border="0" cellspacing="0" cellpadding="0">
《tbody>《tr>
《td colspan="2">《img src="http://www2.blogger.com/image/login/login.gif" width="227" height="45" />《/td>
《/tr>
《tr>
《td colspan="2">
《table width="100%" border="0" cellspacing="0" cellpadding="0">
《tbody>《tr>
《td colspan="3">《/td>
《/tr>
《tr>
《td width="26">《img src="http://www2.blogger.com/image/login/useraccount.gif" width="21" height="25" />《/td>
《td>用户账号《/td>
《td width="158">《input type="text" name="username" size="20" maxlength="66">
《/td>
《/tr>
《tr>
《td width="26">《img src="http://www2.blogger.com/image/login/password.gif" width="21" height="25" />《/td>
《td>用户密码《/td>
《td width="158">《input name="password" type="password" id="password" size="20" maxlength="23">
《/td>
《/tr>
《input type="hidden" name="RecordPassword" value="on">
《input type="hidden" name="authmode" value="CHAP">
《input type="hidden" name="websuserip" value="10.245.113.32">
《input type="hidden" name="challenge" size="50" value="dhefbmmihpnfgmei">
《input type="hidden" name="submittime" value="0">
《/table>
《/td>
《/tr>
《tr>
《td height="17">《div align="center">
《input name="clear" type="button" value="清 除" onmouseout="this.style.backgroundColor='#D6EFFF'" onmouseover="this.style.backgroundColor='#94D8FF'" onclick="'return">《/div>《/td>
《td height="27">《div align="center">

《input name="submit" type="submit" height="27" value="登 录" onmouseout="this.style.backgroundColor='#D6EFFF'" onmouseover="this.style.backgroundColor='#94D8FF'">《/div>《/td>
《/tr>
《/table>
《/form>

可以知道,要做的就是给input成员username和password赋值,然后触发form的submit事件就可以。
不过,我在触发submit时遇到了一些问题,因为submit不是C#提供的常用的已注册事件,所以不能够通过RaiseEvent直接调用。
上穷碧落下黄泉,最后还是在一个国内的网站上找到了答案,可以通过AttachEventHandler("submit",new EventHandler(fun))给form添加事件处理函数fun,然后在fun中用InvokeMember方法调用页面中的响应函数,不过这样可能就不能submit页面到form的action定义的url了(偶没试)。
所以,用了改网站上提供的另外一种方法--使用InvokeMember调用input按钮submit的click事件,也可以达到触发form的submit的目的,虽然比较曲折,不过代码反而更简洁。
最后的C#代码如下:
private void Form1_Load(object sender, EventArgs e)
{
 webBrowser1.Navigate("http://10.245.*.*/"); //打开目标URL
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{ //webBrowser完成页面加载:
 if (webBrowser1.Url.ToString() == "http://10.245.*.*/") //submit后还会加载一次,而所加载的页面“注销”按钮的name也是submit,汗,所以这要判断下
 {
  HtmlDocument doc = webBrowser1.Document; //获取document对象
  HtmlElement btn = null;
  foreach (HtmlElement em in doc.All) //轮循
  {
   string str = em.Name;

   if ((str == "username") || (str == "password") || (str == "submit")) //减少处理
   {
    switch (str)
    {
    case "username": em.SetAttribute("value", "****"); break; //赋用户名
    case "password": em.SetAttribute("value", "****"); break; //赋密码
    case "submit": btn = em; break; //获取submit按钮
    default: break;
    }
   }
  }
  btn.InvokeMember("click"); //触发submit事件
  //doc.Forms["LoginForm"].InvokeMember("submit");
 }
 else //成功登陆后关闭
 {
  this.Close();
 }
}

原创粉丝点击