Capture packets through http to gain an in-depth understanding of ASP. NET webform viewstate

Source: Internet
Author: User

Last week, a website packet capture function developed from ASP. NET webform was completed. This function requires you to use the account in this outlet through our ownProgramObtain data from the website. Httpwebrequest is used for packet capture. The specific packet capture process will not be discussed in detail. The purpose is to share with you some further information on the role of viewstate in ASP. NET webform during packet capture. If there are any deficiencies, we hope you can point them out.

To simulate http post/get, we use vs to create two projects, as shown below:

Note: The first project is a simple ASP. NET web form program, and the second is to simulate the winform program of web form.

Run webapplication1 as follows:

The two server-side controls dropdownlist and button server-side events are as follows:

 
Protected void dropdownlistincluselectedindexchanged (Object sender, eventargs e) {} protected void button#click (Object sender, eventargs e) {If (dropdownlist1.selectedvalue = "two") {lblinfor. TEXT = "two";} else {lblinfor. TEXT = "one ";}}

 

FunctionCodeVery simple. The text value of dropdownlist is displayed after button1 is clicked:

After webform is introduced, winform is used to simulate the web form program through http post/get. The program running interface is as follows:

Here, onepost and twopost simulate the post button click function in webform respectively.

Paste the simulated core code: postbywebrequest function:

Private void postbywebrequest (string strpostvalue) {try {string uri = "http: // localhost: 2026/webform1.aspx/"; httpwebrequest request = webrequest. create (URI) as httpwebrequest; request. method = "get"; request. keepalive = true; request. cookiecontainer = cookiecontainer; httpwebresponse response = request. getresponse () as httpwebresponse; system. io. stream responsestream = response. getresponsestream (); System. io. streamreader reader = new system. io. streamreader (responsestream, encoding. utf8); // returned page HTML text string srcstring = reader. readtoend (); // veiwstate string viewstateflag = "id = \" _ viewstate \ "value = \" "; int len1 = srcstring. indexof (viewstateflag) + viewstateflag. length; int len2 = srcstring. indexof ("\" ", len1); string viewstate = srcstring. substring (len1, len2-len1); // eventvalidation String eventvalidationflag = "id = \" _ eventvalidation \ "value = \" "; len1 = srcstring. indexof (eventvalidationflag) + eventvalidationflag. length; len2 = srcstring. indexof ("\" ", len1); string eventvalidation = srcstring. substring (len1, len2-len1); // encode viewstate = system. web. httputility. urlencode (viewstate); eventvalidation = system. web. httputility. urlencode (eventvalidation); // you can use the packet capture tool to obtain poststrin. G. Remember that urlencode encoding is required for Chinese characters. String formatstring = "dropdownlist1 = {0} & button1 = {1} & __ viewstate = {2} & __ eventvalidation = {3}"; string poststring = string. format (formatstring, strpostvalue, "Do PostBack", viewstate, eventvalidation); byte [] postdata = encoding. utf8.getbytes (poststring); uri = "http: // localhost: 2026/webform1.aspx/"; // POST request = webrequest. create (URI) as httpwebrequest; request. method = "Post"; request. keepalive = false; request. contenttype = "application/X-WWW-form-urlencoded"; request. cookiecontainer = cookiecontainer; request. contentlength = postdata. length; system. io. stream outputstream = request. getrequeststream (); outputstream. write (postdata, 0, postdata. length); outputstream. close (); response = request. getresponse () as httpwebresponse; responsestream = response. getresponsestream (); reader = new system. io. streamreader (responsestream, encoding. utf8); srcstring = reader. readtoend ();} catch (exception ex) {string MSG = ex. message; MessageBox. show (ex. message );}}

The postbywebrequest function is called as follows:

Click the onepost button to execute: postbywebrequest ("one ");

Click the twopost button to execute: postbywebrequest ("two ");

NOTE: IfRow 31st of the postbywebrequest function, without passing in viewstate orEventvalidation, the two buttons in winform cannot successfully simulate webform. The correct result of the final simulation is:Set the breakpoint in the webform button_click, and thenClick the two buttons in winform. vs automatically intercepts the breakpoint settings. This shows that we have successfully simulated the webform post mechanism. If you clickWhen the twopost button is executed, the webform server will not only execute the post button event, but also execute the selectedindexchanged event of the dropdownlist. Why is the selectedindexchanged event on the dropdownlist executed? All of this is attributed to viewstate. It records the content of the webform form and then processes it accordingly. Therefore, the viewstate mechanism of webform is very powerful. Let's use the event mechanism to develop web programs. At the same time, the viewstate mechanism makes it more difficult for our web form program to be "crawler" to get program content. To use the post webform program, we must pass in viewstate. Otherwise, the post will not return the correct page content.

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.