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

时间:2023-03-09 15:52:52
使用C#模拟ASP.NET页面中按钮点击

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

使用C#模拟ASP.NET页面中按钮点击
1 使用C#模拟ASP.NET页面中按钮点击 <% @ Page Language = " C# "  AutoEventWireup = " true "   CodeFile = " Default.aspx.cs "  Inherits = " _Default" %>
2 使用C#模拟ASP.NET页面中按钮点击
3 使用C#模拟ASP.NET页面中按钮点击 <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
4 使用C#模拟ASP.NET页面中按钮点击
5 使用C#模拟ASP.NET页面中按钮点击 < html  xmlns ="http://www.w3.org/1999/xhtml" >
6 使用C#模拟ASP.NET页面中按钮点击 < head  runat ="server" >
7 使用C#模拟ASP.NET页面中按钮点击 < title > 无标题页 </ title >
8 使用C#模拟ASP.NET页面中按钮点击 </ head >
9 使用C#模拟ASP.NET页面中按钮点击 < body >
10 使用C#模拟ASP.NET页面中按钮点击 < form  id ="form1"  runat ="server" >
11 使用C#模拟ASP.NET页面中按钮点击 < div >
12 使用C#模拟ASP.NET页面中按钮点击 < table >
13 使用C#模拟ASP.NET页面中按钮点击 < tr >
14 使用C#模拟ASP.NET页面中按钮点击 < td >  姓名: </ td >< td >< asp:TextBox  ID ="txtName"  runat ="server" ></ asp:TextBox ></ td >
15 使用C#模拟ASP.NET页面中按钮点击 </ tr >
16 使用C#模拟ASP.NET页面中按钮点击 < tr >
17 使用C#模拟ASP.NET页面中按钮点击 < td >  昵称: </ td >< td >< asp:TextBox  ID ="txtPwd"  runat ="server"  TextMode ="Password"  Width="149px" ></ asp:TextBox ></ td >
18 使用C#模拟ASP.NET页面中按钮点击 </ tr >
19 使用C#模拟ASP.NET页面中按钮点击 </ table >
20 使用C#模拟ASP.NET页面中按钮点击 < asp:Button  ID ="btnUpdate"  runat ="server"  Text ="Longon"  OnClick ="btnUpdate_Click"  Width="60px" />
21 使用C#模拟ASP.NET页面中按钮点击 < asp:Button  ID ="btnClose"  runat ="server"  OnClick ="btnClose_Click"  Text ="Close" />< br  />
22 使用C#模拟ASP.NET页面中按钮点击
23 使用C#模拟ASP.NET页面中按钮点击 </ div >
24 使用C#模拟ASP.NET页面中按钮点击 </ form >
25 使用C#模拟ASP.NET页面中按钮点击 </ body >
26 使用C#模拟ASP.NET页面中按钮点击 </ html >

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

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

由上面的代码可以看出除了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页面发出请求,并得到返回数据的类

使用C#模拟ASP.NET页面中按钮点击
  1 使用C#模拟ASP.NET页面中按钮点击 using  System;
  2 使用C#模拟ASP.NET页面中按钮点击 using  System.Net;
  3 使用C#模拟ASP.NET页面中按钮点击
  4 使用C#模拟ASP.NET页面中按钮点击 namespace  Dlse.Com.Cn.Why
  5 使用C#模拟ASP.NET页面中按钮点击 {
  6 使用C#模拟ASP.NET页面中按钮点击 class  WebPageReader
  7 使用C#模拟ASP.NET页面中按钮点击 {
  8 使用C#模拟ASP.NET页面中按钮点击
  9 使用C#模拟ASP.NET页面中按钮点击 /// <summary>
10 使用C#模拟ASP.NET页面中按钮点击 ///  cookie
11 使用C#模拟ASP.NET页面中按钮点击 /// </summary>
12 使用C#模拟ASP.NET页面中按钮点击 private  CookieCollection _Cookies  = new  CookieCollection();
13 使用C#模拟ASP.NET页面中按钮点击
14 使用C#模拟ASP.NET页面中按钮点击 /// <summary>
15 使用C#模拟ASP.NET页面中按钮点击 ///  保持提交到同一个Session
16 使用C#模拟ASP.NET页面中按钮点击 /// </summary>
17 使用C#模拟ASP.NET页面中按钮点击 private  CookieContainer cookieContainer  = new  CookieContainer();
18 使用C#模拟ASP.NET页面中按钮点击
19 使用C#模拟ASP.NET页面中按钮点击 /// <summary>
20 使用C#模拟ASP.NET页面中按钮点击 ///  保持连接
21 使用C#模拟ASP.NET页面中按钮点击 /// </summary>
22 使用C#模拟ASP.NET页面中按钮点击 private bool  isKeepAlive  = false ;
23 使用C#模拟ASP.NET页面中按钮点击
24 使用C#模拟ASP.NET页面中按钮点击 public bool  IsKeepAlive
25 使用C#模拟ASP.NET页面中按钮点击 {
26 使用C#模拟ASP.NET页面中按钮点击 get {  return  isKeepAlive; }
27 使用C#模拟ASP.NET页面中按钮点击 set { isKeepAlive  =  value; }
28 使用C#模拟ASP.NET页面中按钮点击         }
29 使用C#模拟ASP.NET页面中按钮点击
30 使用C#模拟ASP.NET页面中按钮点击
31 使用C#模拟ASP.NET页面中按钮点击
32 使用C#模拟ASP.NET页面中按钮点击 public string  GetHTML( string  URL)
33 使用C#模拟ASP.NET页面中按钮点击 {
34 使用C#模拟ASP.NET页面中按钮点击 return  GetHTML(URL,  "" , System.Text.Encoding.ASCII);
35 使用C#模拟ASP.NET页面中按钮点击         }
36 使用C#模拟ASP.NET页面中按钮点击
37 使用C#模拟ASP.NET页面中按钮点击 public string  GetHTML( string  URL,  string  PostData)
38 使用C#模拟ASP.NET页面中按钮点击 {
39 使用C#模拟ASP.NET页面中按钮点击 return  GetHTML(URL, PostData, System.Text.Encoding.ASCII);
40 使用C#模拟ASP.NET页面中按钮点击         }
41 使用C#模拟ASP.NET页面中按钮点击
42 使用C#模拟ASP.NET页面中按钮点击 public string  GetHTML( string  URL, System.Text.Encoding encoding)
43 使用C#模拟ASP.NET页面中按钮点击 {
44 使用C#模拟ASP.NET页面中按钮点击 return  GetHTML(URL, "" ,encoding );
45 使用C#模拟ASP.NET页面中按钮点击         }
46 使用C#模拟ASP.NET页面中按钮点击
47 使用C#模拟ASP.NET页面中按钮点击 /// <summary>
48 使用C#模拟ASP.NET页面中按钮点击 ///  获取指定地址的html
49 使用C#模拟ASP.NET页面中按钮点击 /// </summary>
50 使用C#模拟ASP.NET页面中按钮点击 /// <param name="URL"></param>
51 使用C#模拟ASP.NET页面中按钮点击 /// <param name="PostData"></param>
52 使用C#模拟ASP.NET页面中按钮点击 /// <param name="encoding"></param>
53 使用C#模拟ASP.NET页面中按钮点击 /// <returns></returns>
54 使用C#模拟ASP.NET页面中按钮点击 public string  GetHTML( string  URL,  string  PostData, System.Text.Encoding encoding)
55 使用C#模拟ASP.NET页面中按钮点击 {
56 使用C#模拟ASP.NET页面中按钮点击             isKeepAlive  = false ;
57 使用C#模拟ASP.NET页面中按钮点击 string  _Html  = "" ;
58 使用C#模拟ASP.NET页面中按钮点击
59 使用C#模拟ASP.NET页面中按钮点击             HttpWebRequest request  =  (HttpWebRequest)HttpWebRequest.Create(URL);
60 使用C#模拟ASP.NET页面中按钮点击             request.Accept  = " image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */* " ;
61 使用C#模拟ASP.NET页面中按钮点击
62 使用C#模拟ASP.NET页面中按钮点击
63 使用C#模拟ASP.NET页面中按钮点击 if  (_Cookies.Count  > 0 )
64 使用C#模拟ASP.NET页面中按钮点击 {
65 使用C#模拟ASP.NET页面中按钮点击                 request.CookieContainer.Add( new  Uri(URL), _Cookies);
66 使用C#模拟ASP.NET页面中按钮点击             }
67 使用C#模拟ASP.NET页面中按钮点击 else
68 使用C#模拟ASP.NET页面中按钮点击 {
69 使用C#模拟ASP.NET页面中按钮点击                 request.CookieContainer  = this .cookieContainer;
70 使用C#模拟ASP.NET页面中按钮点击             }
71 使用C#模拟ASP.NET页面中按钮点击
72 使用C#模拟ASP.NET页面中按钮点击 // 提交的数据
73 使用C#模拟ASP.NET页面中按钮点击 if  (PostData  != null &&  PostData.Length  > 0 )
74 使用C#模拟ASP.NET页面中按钮点击 {
75 使用C#模拟ASP.NET页面中按钮点击                 request.ContentType  = " application/x-www-form-urlencoded " ;
76 使用C#模拟ASP.NET页面中按钮点击                 request.Method  = " POST " ;
77 使用C#模拟ASP.NET页面中按钮点击
78 使用C#模拟ASP.NET页面中按钮点击 byte [] b  =  encoding.GetBytes(PostData);
79 使用C#模拟ASP.NET页面中按钮点击                 request.ContentLength  =  b.Length;
80 使用C#模拟ASP.NET页面中按钮点击 using  (System.IO.Stream sw  =  request.GetRequestStream())
81 使用C#模拟ASP.NET页面中按钮点击 {
82 使用C#模拟ASP.NET页面中按钮点击 try
83 使用C#模拟ASP.NET页面中按钮点击 {
84 使用C#模拟ASP.NET页面中按钮点击                         sw.Write(b,  0 , b.Length);
85 使用C#模拟ASP.NET页面中按钮点击                     }
86 使用C#模拟ASP.NET页面中按钮点击 catch  (Exception ex)
87 使用C#模拟ASP.NET页面中按钮点击 {
88 使用C#模拟ASP.NET页面中按钮点击 throw new  Exception( " Post Data Error!! " , ex);
89 使用C#模拟ASP.NET页面中按钮点击                     }
90 使用C#模拟ASP.NET页面中按钮点击 finally
91 使用C#模拟ASP.NET页面中按钮点击 {
92 使用C#模拟ASP.NET页面中按钮点击 if  (sw  != null )  { sw.Close(); }
93 使用C#模拟ASP.NET页面中按钮点击                     }
94 使用C#模拟ASP.NET页面中按钮点击                 }
95 使用C#模拟ASP.NET页面中按钮点击             }
96 使用C#模拟ASP.NET页面中按钮点击
97 使用C#模拟ASP.NET页面中按钮点击
98 使用C#模拟ASP.NET页面中按钮点击             HttpWebResponse response  = null ;
99 使用C#模拟ASP.NET页面中按钮点击             System.IO.StreamReader sr  = null ;
100 使用C#模拟ASP.NET页面中按钮点击
101 使用C#模拟ASP.NET页面中按钮点击 try
102 使用C#模拟ASP.NET页面中按钮点击 {
103 使用C#模拟ASP.NET页面中按钮点击
104 使用C#模拟ASP.NET页面中按钮点击                 response  =  (HttpWebResponse)request.GetResponse();
105 使用C#模拟ASP.NET页面中按钮点击
106 使用C#模拟ASP.NET页面中按钮点击                 _Cookies  =  response.Cookies;
107 使用C#模拟ASP.NET页面中按钮点击
108 使用C#模拟ASP.NET页面中按钮点击                 sr  = new  System.IO.StreamReader(response.GetResponseStream(), encoding);
109 使用C#模拟ASP.NET页面中按钮点击
110 使用C#模拟ASP.NET页面中按钮点击                 _Html  =  sr.ReadToEnd();
111 使用C#模拟ASP.NET页面中按钮点击
112 使用C#模拟ASP.NET页面中按钮点击             }
113 使用C#模拟ASP.NET页面中按钮点击 catch  (WebException webex)
114 使用C#模拟ASP.NET页面中按钮点击 {
115 使用C#模拟ASP.NET页面中按钮点击 if  (webex.Status  ==  WebExceptionStatus.KeepAliveFailure)
116 使用C#模拟ASP.NET页面中按钮点击 {
117 使用C#模拟ASP.NET页面中按钮点击                     isKeepAlive  = true ;
118 使用C#模拟ASP.NET页面中按钮点击                 }
119 使用C#模拟ASP.NET页面中按钮点击 else
120 使用C#模拟ASP.NET页面中按钮点击 {
121 使用C#模拟ASP.NET页面中按钮点击 throw new  Exception( " DownLoad Data Error " , webex);
122 使用C#模拟ASP.NET页面中按钮点击                 }
123 使用C#模拟ASP.NET页面中按钮点击             }
124 使用C#模拟ASP.NET页面中按钮点击 catch  (System.Exception ex)
125 使用C#模拟ASP.NET页面中按钮点击 {
126 使用C#模拟ASP.NET页面中按钮点击 throw new  Exception( " DownLoad Data Error " , ex);
127 使用C#模拟ASP.NET页面中按钮点击             }
128 使用C#模拟ASP.NET页面中按钮点击 finally
129 使用C#模拟ASP.NET页面中按钮点击 {
130 使用C#模拟ASP.NET页面中按钮点击 if  (sr  != null )  { sr.Close(); }
131 使用C#模拟ASP.NET页面中按钮点击 if  (response  != null )  { response.Close(); }
132 使用C#模拟ASP.NET页面中按钮点击                 response  = null ;
133 使用C#模拟ASP.NET页面中按钮点击                 request  = null ;
134 使用C#模拟ASP.NET页面中按钮点击             }
135 使用C#模拟ASP.NET页面中按钮点击
136 使用C#模拟ASP.NET页面中按钮点击 return  _Html;
137 使用C#模拟ASP.NET页面中按钮点击
138 使用C#模拟ASP.NET页面中按钮点击         }
139 使用C#模拟ASP.NET页面中按钮点击     }
140 使用C#模拟ASP.NET页面中按钮点击 }
141 使用C#模拟ASP.NET页面中按钮点击

使用方法如下

使用C#模拟ASP.NET页面中按钮点击
1 使用C#模拟ASP.NET页面中按钮点击 private  WebPageReader webReader = new  WebPageReader();
2 使用C#模拟ASP.NET页面中按钮点击
3 使用C#模拟ASP.NET页面中按钮点击 string  __VIEWSTATE  = " /wEPDwUKMTg4ODA4NDE0NmRk6Ma0MaCJKrrNLGLfO4qYNezoxY4= " ;
4 使用C#模拟ASP.NET页面中按钮点击 string  __EVENTVALIDATION  = "/wEWBQKcopufDgLEhISFCwKd+7qdDgLynailDAKT+PmaCJleqITXMfQuE9LK49YoxHV2oTzQ " ;
5 使用C#模拟ASP.NET页面中按钮点击
6 使用C#模拟ASP.NET页面中按钮点击 __VIEWSTATE  =  System.Web.HttpUtility.UrlEncode(__VIEWSTATE);
7 使用C#模拟ASP.NET页面中按钮点击
8 使用C#模拟ASP.NET页面中按钮点击 __EVENTVALIDATION  =  System.Web.HttpUtility.UrlEncode(__EVENTVALIDATION);
9 使用C#模拟ASP.NET页面中按钮点击
10 使用C#模拟ASP.NET页面中按钮点击 string  strPostData  =  String.Format( " __VIEWSTATE={0}&txtName={1}&txtPwd={2}&btnUpdate=Longon&__EVENTVALIDATION={3} "
11 使用C#模拟ASP.NET页面中按钮点击                             , __VIEWSTATE,  this .txtName.Text,  this .txtPassword.Text, __EVENTVALIDATION
12 使用C#模拟ASP.NET页面中按钮点击                             );
13 使用C#模拟ASP.NET页面中按钮点击 string  strHTML;
14 使用C#模拟ASP.NET页面中按钮点击
15 使用C#模拟ASP.NET页面中按钮点击 try
16 使用C#模拟ASP.NET页面中按钮点击 {
17 使用C#模拟ASP.NET页面中按钮点击 do
18 使用C#模拟ASP.NET页面中按钮点击 {
19 使用C#模拟ASP.NET页面中按钮点击         strHTML  =  webReader.GetHTML( " http://localhost:3517/WebSite1/Default.aspx " , strPostData);
20 使用C#模拟ASP.NET页面中按钮点击     } while  (webReader.IsKeepAlive);
21 使用C#模拟ASP.NET页面中按钮点击
22 使用C#模拟ASP.NET页面中按钮点击
23 使用C#模拟ASP.NET页面中按钮点击 this .richTextBox1.Text  =  strHTML;
24 使用C#模拟ASP.NET页面中按钮点击 }
25 使用C#模拟ASP.NET页面中按钮点击 catch  (Exception ex)
26 使用C#模拟ASP.NET页面中按钮点击 {
27 使用C#模拟ASP.NET页面中按钮点击 if  (ex.InnerException  != null )
28 使用C#模拟ASP.NET页面中按钮点击 {
29 使用C#模拟ASP.NET页面中按钮点击         MessageBox.Show(ex.Message  + " /n " +  ex.InnerException.Message);
30 使用C#模拟ASP.NET页面中按钮点击     }
31 使用C#模拟ASP.NET页面中按钮点击 else
32 使用C#模拟ASP.NET页面中按钮点击 {
33 使用C#模拟ASP.NET页面中按钮点击         MessageBox.Show(ex.Message);
34 使用C#模拟ASP.NET页面中按钮点击     }
35 使用C#模拟ASP.NET页面中按钮点击 }