C#自动填表和关于WebBrowserSubmit

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成员usernamepassword赋值,然后触发formsubmit事件就可以。

不过,我在触发submit时遇到了一些问题,因为submit不是C#提供的常用的已注册事件,所以不能够通过RaiseEvent直接调用。

上穷碧落下黄泉,最后还是在一个国内的网站上找到了答案,可以通过AttachEventHandler("submit",new EventHandler(fun))form添加事件处理函数fun,然后在fun中用InvokeMember方法调用页面中的响应函数,不过这样可能就不能submit页面到formaction定义的url了(偶没试)。

所以,用了改网站上提供的另外一种方法--使用InvokeMember调用input按钮submitclick事件,也可以达到触发formsubmit的目的,虽然比较曲折,不过代码反而更简洁。

最后的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();

}

}

posted @ 2009-07-10 17:18  小师傅  阅读(567)  评论(0编辑  收藏  举报