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:
- // URI string of the form to be submitted.
- StringuriString=Http://www.xxx.com/Login.aspx";
- // String data to be submitted.
- StringpostString="UserName = user1 & password = password1";
- // Initialize WebClient
- WebClientwebClient=NewWebClient();
- WebClient. Headers. Add ("Content-Type", "application/x-www-form-urlencoded ");
- // Convert the string into a byte array
- Byte []PostData=Encoding. ASCII. GetBytes (postString );
- // Upload data and return the byte array of the page
- Byte []ResponseData=WebClient. UploadData (uriString, "POST", postData );
- // The returned byte array is converted into a string (HTML)
- 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:
- id="__VIEWSTATE"value="/wEPDwUKMTg0NTgwMzM2M2RksjXHwIOzdq/skwDy1k6qTexm2j0="
- 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.
- viewState=System.Web.HttpUtility.UrlEncode(viewState);
- eventValidation=System.Web.HttpUtility.UrlEncode(eventValidation);
The complete code is as follows:
- // Text of the submit button
- StringsubmitButton="Login";
- // The VeiwState of the page can be opened through IE, right-click "View Source File)
- StringviewState="/Wepdwukmt1_ntgwmzm2m2rksjxhwiozdq/skwDy1k6qTexm2j0 =";
- // EventValidation of the page can be opened through IE and obtained by right-clicking "View Source File)
- StringeventValidation=
"/WEWBAKxhbOEAQKPpuq2CALyveCRDwLejM6fDwP2723lUdzBJVBIAVzbpM2sXYqc";
-
- SubmitButton=System. Web. HttpUtility. UrlEncode (submitButton );
- ViewState=System. Web. HttpUtility. UrlEncode (viewState );
- EventValidation=System. Web. HttpUtility. UrlEncode (eventValidation );
-
- Try
- {
- // URI string of the form to be submitted.
- StringuriString=Http://www.xxx.com/Login.aspx";
- // String data to be submitted. Format: User=Uesr1&Password=123
- StringpostString="UserName = 1 & password = 1"+ "&LoginButton="+ SubmitButton +"
&_ VIEWSTATE="+ ViewState +"&_ EVENTVALIDATION= "+ EventValidation;
- // Initialize WebClient
- WebClientwebClient=NewWebClient();
- WebClient. Headers. Add ("Content-Type", "application/x-www-form-urlencoded ");
- // Convert the string into a byte array
- Byte []PostData=Encoding. ASCII. GetBytes (postString );
- // Upload data and return the byte array of the page
- Byte []ResponseData=WebClient. UploadData (uriString, "POST", postData );
- // Convert the returned byte array into a string (HTML );
- // The pages returned by ASP. NET are generally Unicode.
- // Encoding. GetEncoding ("GB2312"). GetString (responseData)
- StringsrcString=Encoding. UTF8.GetString (responseData );
- }
- Catch (WebExceptionwe)
- {
- Stringmsg=We. Message;
- }
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.
- Analysis of Theme functions in ASP. NET development skills
- ASP. NET Dynamic Compilation
- Analysis on ASP. NET supported by Apache
- Introduction to ASP. NET Server standard controls
- Analysis on SQL Server Database Backup Recovery in ASP. NET