[Go] C # simulates a click of a button in an ASP. NET page, making a request to the Web server

Source: Internet
Author: User
Tags urlencode

In the absence of the content described in the topic, it is supposed to be a very simple thing, but when really started to find that there are many problems
Now write it down here, for the same level of reference as I do.

Refer to the article before writing this article

Welcome to CSDN Forum Reader: CSDN Reader (with full source code) Latest version: 20070212
Http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html

C # keeps the post data in the same session through WebRequest http://blog.csdn.net/crabo/archive/2005/12/08/547092.aspx

C # Simulates a click of a button in an ASP. NET page, making a request to the Web server
It is primarily about organizing the data to be submitted and then submitting it as post.

Suppose we have the following page


1<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
2
3<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
4
567 <title> Untitled Page </title>
89<body>
Ten <form id= "Form1" runat= "Server" >
<div>
<table>
<tr>
<td> Name: </td><td><asp:textbox id= "txtname" runat= "Server" &GT;&LT;/ASP:TEXTBOX&GT;&LT;/TD&G T
</tr>
<tr>
<td> Nickname: </td><td><asp:textbox id= "txtpwd" runat= "Server" textmode= "Password" width= "149px" & Gt;</asp:textbox></td>
</tr>
</table>
<asp:button id= "btnupdate" runat= "Server" text= "Longon" onclick= "Btnupdate_click" width= "60px"/>
<asp:button id= "btnclose" runat= "Server" onclick= "btnClose_Click" text= "Close"/><br/>
22
</div>
</form>
25</body>
26

When you use IE to access this page, you can get the following output


1
2
3<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
4
567 Untitled Page
8</title>9<body>
<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
<div>
<table>
<tr>
<td> Name: </td><td><input name= "txtname" type= "text" id= "Txtname"/></td>
</tr>
<tr>
<td> Nickname: </td><td><input name= "txtpwd" type= "password" id= "txtpwd" style= "WIDTH:149PX;"/&gt ;</td>
</tr>
</table>
<input type= "Submit" Name= "Btnupdate" value= "Logon" id= "btnupdate" style= "width:60px;"/>
<input type= "Submit" Name= "Btnclose" value= "Close" id= "btnclose"/><br/>
26
</div>
28
29<div>
30
<input type= "hidden" name= "__eventvalidation" id= "__eventvalidation" value= "/wewbqkcopufdglehisfcwkd+ 7qddglynaildakt+pmacjleqitxmfque9lk49yoxhv2otzq "/>
32</div></form>
33</body>
3435


From the above code can be seen in addition to Txtname,txtpwd and two buttons, more than two __viewstate,__eventvalidation these four forms need to be submitted to, to simulate which button, which button is added to the value of the form, such as: Btnupdate=logon

When stitching the submitted string, note that the System.Web.HttpUtility.UrlEncode method is used to convert the URL-encoded string.
Here is the submission data for the btnupdate button on this page

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


Then create a HttpWebRequest object, set the Commit method to post, and then write the string prepared above into the request data stream
Basically, yes.
If you need to save the session when accessing different pages, you need to set the Cookiecontainer property of the HttpWebRequest object to ensure that each set of Cookiecontainer is the same object.
Here is the class that makes the request to the Web page and gets the returned data

read Web page content


Use the following method


1private Webpagereader webreader = new Webpagereader ();
2
3string __viewstate = "/wepdwukmtg4oda4nde0nmrk6ma0macjkrrnlglfo4qynezoxy4=";
4string __eventvalidation = "/wewbqkcopufdglehisfcwkd+7qddglynaildakt+pmacjleqitxmfque9lk49yoxhv2otzq";
5
6__viewstate = System.Web.HttpUtility.UrlEncode (__viewstate);
7
8__eventvalidation = System.Web.HttpUtility.UrlEncode (__eventvalidation);
9
10string strpostdata = String.Format ("__viewstate={0}&txtname={1}&txtpwd={2}&btnupdate=longon&__ EVENTVALIDATION={3} "
One, __viewstate, This.txtName.Text, This.txtPassword.Text, __eventvalidation
12);
13string strhtml;
14
15try
16{
+ Do
18{
strHTML = webreader.gethtml ("http://localhost:3517/WebSite1/Default.aspx", strpostdata);
(webreader.iskeepalive);
21st
22
This.richTextBox1.Text = strhtml;
24}
25catch (Exception ex)
26{
if (ex. InnerException = null)
{
MessageBox.Show (ex. Message + "/n" + ex. Innerexception.message);
30}
-Else
{
MessageBox.Show (ex. Message);
34}
+}

[Go] C # simulates a click of a button in an ASP. NET page, making a request to the Web server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.