C#自動填表和關於WebBrowserSubmit

來源:互聯網
上載者:User

用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();

}

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.