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

Source: Internet
Author: User

In. netProgramIt is relatively simple to enter and submit a form. For example, to submit a logon form as shown in:

Enter and submit the preceding formCodeAs follows:

// Uri string of the form to be submitted.
String Uristring =   " Http://www.xxx.com/Login.aspx " ;
// String data to be submitted.
String Poststring =   " Username = user1 & Password = password1 " ;
// Initialize WebClient
WebClient =   New WebClient ();
WebClient. headers. Add ( " Content-Type " , " Application/X-WWW-form-urlencoded " );
// Converts a string to 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 );
// Converts a byte array to a string (HTML)
String Srcstring = 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 forms. When submitting an ASP. NET 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 = "/wepdwukmt1_ntgwmzm2rksjxhwiozdq/skwdy1k6qtexm2j0 ="
Id = "_ eventvalidation" value = "/wewbakxhboeaqkppuq2calyvecrdwlejm6fdwp2723ludzbjvbiavzbpm2sxyqc"
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
String Submitbutton =   " Login " ;
// Veiwstate of the page (you can open the page through IE and right-click "View Source File" to get it)
String Viewstate =   " /Wepdwukmt1_ntgwmzm2rksjxhwiozdq/skwdy1k6qtexm2j0 = " ;
// Eventvalidation of the page (you can open the page through IE and right-click "View Source File" to get it)
String Eventvalidation =   " /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.
String Uristring =   " Http://www.xxx.com/Login.aspx " ;
// String data to be submitted. Format: User = uesr1 & Password = 123
String Poststring =   " Username = 1 & Password = 1 "   +   " & Loginbutton = "   + Submitbutton +   " & __ Viewstate = "   + Viewstate +   " & __ Eventvalidation = "   + Eventvalidation;
// Initialize WebClient
WebClient =   New WebClient ();
WebClient. headers. Add ( " Content-Type " , " Application/X-WWW-form-urlencoded " );
// Converts a string to 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 );
// Converts the returned byte array into a string (HTML );
// The pages returned by ASP. NET are generally Unicode.
// Encoding. getencoding ("gb2312"). getstring (responsedata)
String Srcstring = Encoding. utf8.getstring (responsedata );
}
Catch (Webexception we)
{
String MSG = 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.

RelatedArticle: Use WebClient to automatically fill in and submit the ASP. NET page formSource code
Use regular expressions in C # To automatically match and obtain the required data.

Address: http://www.cnblogs.com/anjou/archive/2006/12/25/602943.html
 

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.