您的位置:首页 > 编程语言 > ASP

使用C#模拟ASP.NET页面中按钮点击

2014-07-12 17:08 681 查看


c# 模拟Asp.net页面中的某个按钮的点击,向web服务器发出请求
主要就组织要提交的数据,然后以post方式提交。
假设我们有如下的网页

3 <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
4
5 < html xmlns ="http://www.w3.org/1999/xhtml" >
6 < head runat ="server" >
7 < title > 无标题页 </ title >
8 </ head >
9 < body >
10 < form id ="form1" runat ="server" >
11 < div >
12 < table >
13 < tr >
14 < td > 姓名: </ td >< td >< asp:TextBox ID ="txtName" runat ="server" ></ asp:TextBox ></ td >
15 </ tr >
16 < tr >
17 < td > 昵称: </ td >< td >< asp:TextBox ID ="txtPwd" runat ="server" TextMode ="Password" Width="149px" ></ asp:TextBox ></ td >
18 </ tr >
19 </ table >
20 < asp:Button ID ="btnUpdate" runat ="server" Text ="Longon" OnClick ="btnUpdate_Click" Width="60px" />
21 < asp:Button ID ="btnClose" runat ="server" OnClick ="btnClose_Click" Text ="Close" />< br />
22
23 </ div >
24 </ form >
25 </ body >
26 </ html >

用IE访问这个页面的时候可以得到如下的输出

2
3 <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
4
5 < html xmlns ="http://www.w3.org/1999/xhtml" >
6 < head >< title >
7 无标题页
8 </ title ></ head >
9 < body >
10 < form name ="form1" method ="post" action ="Default.aspx" id ="form1" >
11 < div >
12 < input type ="hidden" name ="__VIEWSTATE" id ="__VIEWSTATE" value="/wEPDwUKMTg4ODA4NDE0NmRk6Ma0MaCJKrrNLGLfO4qYNezoxY4=" />
13 </ div >
14
15 < div >
16 < table >
17 < tr >
18 < td > 姓名: </ td >< td >< input name ="txtName" type ="text" id ="txtName" /></ td >
19 </ tr >
20 < tr >
21 < td > 昵称: </ td >< td >< input name ="txtPwd" type ="password" id ="txtPwd" style ="width:149px;" /></ td >
22 </ tr >
23 </ table >
24 < input type ="submit" name ="btnUpdate" value ="Logon" id ="btnUpdate" style ="width:60px;" />
25 < input type ="submit" name ="btnClose" value ="Close" id ="btnClose" />< br />
26
27 </ div >
28
29 < div >
30
31 < input type ="hidden" name ="__EVENTVALIDATION" id ="__EVENTVALIDATION" value="/wEWBQKcopufDgLEhISFCwKd+7qdDgLynailDAKT+PmaCJleqITXMfQuE9LK49YoxHV2oTzQ" />
32 </ div ></ form >
33 </ body >
34 </ html >
35

由上面的代码可以看出除了txtName,txtPwd以及两个按钮外,多出了两个__VIEWSTATE,__EVENTVALIDATION这四个表单需要提交到的,要模拟哪个按钮,在加上哪个按钮的表单的值就可以了,如:btnUpdate=Logon
在拼接提交的字符串的时候注意一下,用System.Web.HttpUtility.UrlEncode方法转换成Url编码的字符串。
下面是针对这个页面的btnUpdate 按钮的提交数据

1 string __VIEWSTATE = " /wEPDwUKMTg4ODA4NDE0NmRk6Ma0MaCJKrrNLGLfO4qYNezoxY4= " ;
2 string __EVENTVALIDATION = "/wEWBQKcopufDgLEhISFCwKd+7qdDgLynailDAKT+PmaCJleqITXMfQuE9LK49YoxHV2oTzQ " ;
3
4 __VIEWSTATE = System.Web.HttpUtility.UrlEncode(__VIEWSTATE);
5
6 __EVENTVALIDATION = System.Web.HttpUtility.UrlEncode(__EVENTVALIDATION);
7
8 string strPostData = String.Format( " __VIEWSTATE={0}&txtName={1}&txtPwd={2}&btnUpdate=Longon&__EVENTVALIDATION={3} "
9 , __VIEWSTATE, this .txtName.Text, this .txtPassword.Text, __EVENTVALIDATION
10 );

然后创建一个HttpWebRequest对象,设置提交方式是post,然后把上面准备的字符串写进请求数据流里
基本上就可以了
如果有需要在访问不同页面时保存Session的话,需要设置HttpWebRequest对象的CookieContainer属性,保证每次设置的CookieContainer都是同一个对象就可以了。
下面是这个类就是向WEB页面发出请求,并得到返回数据的类

using System;
2 using System.Net;
3
4 namespace Dlse.Com.Cn.Why
5

使用方法如下

private WebPageReader webReader = new WebPageReader();
2
3 string __VIEWSTATE = " /wEPDwUKMTg4ODA4NDE0NmRk6Ma0MaCJKrrNLGLfO4qYNezoxY4= " ;
4 string __EVENTVALIDATION = "/wEWBQKcopufDgLEhISFCwKd+7qdDgLynailDAKT+PmaCJleqITXMfQuE9LK49YoxHV2oTzQ " ;
5
6 __VIEWSTATE = System.Web.HttpUtility.UrlEncode(__VIEWSTATE);
7
8 __EVENTVALIDATION = System.Web.HttpUtility.UrlEncode(__EVENTVALIDATION);
9
10 string strPostData = String.Format( " __VIEWSTATE={0}&txtName={1}&txtPwd={2}&btnUpdate=Longon&__EVENTVALIDATION={3} "
11 , __VIEWSTATE, this .txtName.Text, this .txtPassword.Text, __EVENTVALIDATION
12 );
13 string strHTML;
14
15 try
16 catch (Exception ex)
26 {
27 if (ex.InnerException != null )
28 {
29 MessageBox.Show(ex.Message + " /n " + ex.InnerException.Message);
30 }
31 else
32 {
33 MessageBox.Show(ex.Message);
34 }
35 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐