Simulation using code page requests and data submitted

Program code to simulate and submit the request web form data, I use here is a login method. code show as below:
Request the HttpWebRequest  =  (the HttpWebRequest) the WebRequest.Create ( @ http://www.tempuri.com/checkLogin );
String  parms  = String .Format ( " txtUser & txtPass = {0} = {}. 1 = & Submit " , _username, _password) ; byte [] data  =  System.Text.Encoding.GetEncoding ( " GB2312 " ) .GetBytes (parms); // add the following two codes, retains Session log data generated, to other pages will not be prompted to log on without the success of the CookieContainer the cookie  = new new  the CookieContainer (); request.CookieContainer  =  the cookie; // set the format for the submission POST request.method   



 



=   " POST " ;
request.ContentType 
=   " application/x-www-form-urlencoded " ;
request.ContentLength 
=  data.Length;

Stream writer 
=  request.GetRequestStream();
writer.Write(data, 
0 , data.Length);
writer.Close();

// 获取请求回复数据
HttpWebResponse response  =  (HttpWebResponse)request.GetResponse();
StreamReader reader 
=   new  StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding( " GB2312 " ));
string  content  =  reader.ReadToEnd();

return  content;

It is worth noting that several points:
1, the url parameter must be used in WebRequest.Create action is a form of address that the authors address the actual.
2, the author parameter, the second line as described above, must take the form of clear text boxes, etc. to collect those hidden also write on the input, and maintaining a unified name
3, if the simulated log is generally in the login process in the application will write to the cookie or Session in the second paragraph above is used to hold Session on behalf of the data, if the code is not instantly successful login to the application of other pages, will prompt not logged (because verification can not login). If you just submit the form data, the second paragraph of the code can be omitted.
After obtaining the data, how to deal with it is not the focus here.

Reproduced in: https: //www.cnblogs.com/baoposhou/archive/2008/04/25/1170603.html

Guess you like

Origin blog.csdn.net/weixin_33724659/article/details/93320766