The code to fill in and submit the above form is as follows:
// URI string to submit the form.
string uriString = " http://www.xxx.com/Login.aspx ";
// String data to submit.
string postString = "userName=user1&password=password1";
//Initialize WebClient
WebClient webClient = new WebClient();
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
//Convert string to 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)
string srcString = Encoding.UTF8.GetString(responseData);
srcStrinig is the HTML of the page returned after submitting the form. How about it? It's very simple.
However, the above code can submit the form generated by ASP or JSP, but cannot submit the ASP.NET form. Because when submitting an ASP.NET form, values must be assigned to "__VIEWSTATE" and "__EVENTVALIDATION". The values of "__VIEWSTATE" and "__EVENTVALIDATION" can be found by right-clicking "View Source" on the page to be submitted. as follows:
id="__VIEWSTATE" value="/wEPDwUKMTg0NTgwMzM2M2RksjXHwIOzdq/skwDy1k6qTexm2j0="
id="__EVENTVALIDATION" value="/wEWBAKxhbOEAQKPpuq2CALyveCRDwLejM6fDwP2723lUdzBJVBIAVzbpM2sXYqc"
The values of "__VIEWSTATE" and "__EVENTVALIDATION" obtained through "View Source File" cannot be directly submitted to the form and need to be converted into URL-encoded strings.
viewState = System.Web.HttpUtility.UrlEncode(viewState);
eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation);
The complete code is as follows:
// Text of submit button
string submitButton = "Login";
//VeiwState of the page (can be obtained by opening the page through IE, right-clicking "View Source File")
string viewState = "/wEPDwUKMTg0NTgwMzM2M2RksjXHwIOzdq/skwDy1k6qTexm2j0=";
//EventValidation of the page (can be obtained by opening the page through IE, right-clicking "View Source File")
string eventValidation = "/wEWBAKxhbOEAQKPpuq2CALyveCRDwLejM6fDwP2723lUdzBJVBIAVzbpM2sXYqc";
submitButton = System.Web.HttpUtility.UrlEncode(submitButton);
viewState = System.Web.HttpUtility.UrlEncode(viewState);
eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation);
try
{
// URI string to submit the form.
string uriString = " http://www.xxx.com/Login.aspx ";
// String data to submit. The format is as follows: user=uesr1&password=123
string postString = "userName=1&password=1" + "&loginButton=" + submitButton + "&__VIEWSTATE=" + viewState + "&__EVENTVALIDATION=" + eventValidation;
//Initialize WebClient
WebClient webClient = new WebClient();
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
//Convert string to 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 page returned by ASP.NET is generally Unicode. If it is Simplified Chinese, it should be used
// Encoding.GetEncoding("GB2312").GetString(responseData)
string srcString = Encoding.UTF8.GetString(responseData);
}
catch (WebException we)
{
string msg = we.Message;
}
A few notes:
1) srcStrinig is the HTML of the page returned after submitting the form. You can use regular expressions to analyze it to obtain the data you need.
2) The values of "__VIEWSTATE" and "__EVENTVALIDATION" are not static.
3) You can also view webpage POST data through some tools, such as: webpage data analysis tool HttpWatch, network sniffer, etc.
4) If the submitted form has a verification code, it is not within the scope of this article.
URL of this article: http://www.cnblogs.com/anjou/archive/2006/12/25/602943.html