Describe ASP. NET page forms

Source: Internet
Author: User

Use WebClient to automatically fill in and submit ASP. NET page forms

In. NET, it is relatively simple to enter and submit forms through a program. For example, to submit a logon form as shown in:

 
 
  1. // URI string of the form to be submitted.
  2. StringuriString=Http://www.xxx.com/Login.aspx";
  3. // String data to be submitted.
  4. StringpostString="UserName = user1 & password = password1";
  5. // Initialize WebClient
  6. WebClientwebClient=NewWebClient();
  7. WebClient. Headers. Add ("Content-Type", "application/x-www-form-urlencoded ");
  8. // Convert the string into a byte array
  9. Byte []PostData=Encoding. ASCII. GetBytes (postString );
  10. // Upload data and return the byte array of the page
  11. Byte []ResponseData=WebClient. UploadData (uriString, "POST", postData );
  12. // The returned byte array is converted into a string (HTML)
  13. StringsrcString=Encoding. UTF8.GetString (responseData );

SrcStrinig is the HTML of the page returned after the form is submitted. It's easy.

However, the above Code can submit forms generated by ASP or JSP, but cannot submit ASP. NET page forms. When submitting an ASP. NET page form, you must assign values to "_ VIEWSTATE" and "_ EVENTVALIDATION. The values of "_ VIEWSTATE" and "_ EVENTVALIDATION" can be found by right-clicking "View Source File" on the page to be submitted. As follows:

 
 
  1. id="__VIEWSTATE"value="/wEPDwUKMTg0NTgwMzM2M2RksjXHwIOzdq/skwDy1k6qTexm2j0="  
  2. id="__EVENTVALIDATION" 

The values of "_ VIEWSTATE" and "_ EVENTVALIDATION" obtained from "View Source File" cannot be directly submitted to the form, and must be converted to a URL-encoded string.

 
 
  1. viewState=System.Web.HttpUtility.UrlEncode(viewState);  
  2. eventValidation=System.Web.HttpUtility.UrlEncode(eventValidation); 

The complete code is as follows:

 
 
  1. // Text of the submit button
  2. StringsubmitButton="Login";
  3. // The VeiwState of the page can be opened through IE, right-click "View Source File)
  4. StringviewState="/Wepdwukmt1_ntgwmzm2m2rksjxhwiozdq/skwDy1k6qTexm2j0 =";
  5. // EventValidation of the page can be opened through IE and obtained by right-clicking "View Source File)
  6. StringeventValidation=
    "/WEWBAKxhbOEAQKPpuq2CALyveCRDwLejM6fDwP2723lUdzBJVBIAVzbpM2sXYqc";
  7.  
  8. SubmitButton=System. Web. HttpUtility. UrlEncode (submitButton );
  9. ViewState=System. Web. HttpUtility. UrlEncode (viewState );
  10. EventValidation=System. Web. HttpUtility. UrlEncode (eventValidation );
  11.  
  12. Try
  13. {
  14. // URI string of the form to be submitted.
  15. StringuriString=Http://www.xxx.com/Login.aspx";
  16. // String data to be submitted. Format: User=Uesr1&Password=123 
  17. StringpostString="UserName = 1 & password = 1"+ "&LoginButton="+ SubmitButton +"
    &_ VIEWSTATE="+ ViewState +"&_ EVENTVALIDATION= "+ EventValidation;
  18. // Initialize WebClient
  19. WebClientwebClient=NewWebClient();
  20. WebClient. Headers. Add ("Content-Type", "application/x-www-form-urlencoded ");
  21. // Convert the string into a byte array
  22. Byte []PostData=Encoding. ASCII. GetBytes (postString );
  23. // Upload data and return the byte array of the page
  24. Byte []ResponseData=WebClient. UploadData (uriString, "POST", postData );
  25. // Convert the returned byte array into a string (HTML );
  26. // The pages returned by ASP. NET are generally Unicode.
  27. // Encoding. GetEncoding ("GB2312"). GetString (responseData)
  28. StringsrcString=Encoding. UTF8.GetString (responseData );
  29. }
  30. Catch (WebExceptionwe)
  31. {
  32. Stringmsg=We. Message;
  33. }

Notes:
1) srcStrinig is the HTML of the page returned after the form is submitted. You can use regular expressions to analyze the page to obtain the required data.
2) The values of "_ VIEWSTATE" and "_ EVENTVALIDATION" are not static.
3) You can also use some tools to view POST data, such as HttpWatch and network sniffer.
4) if the submitted form has a verification code, it is not covered in this article. The above describes ASP. NET page forms.

  1. Analysis of Theme functions in ASP. NET development skills
  2. ASP. NET Dynamic Compilation
  3. Analysis on ASP. NET supported by Apache
  4. Introduction to ASP. NET Server standard controls
  5. Analysis on SQL Server Database Backup Recovery in ASP. NET

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.