Code highlights:
1. append a cookiecontainer to the httprequest object to obtain the cookie that represents the session ID returned after logon.
2. If the cookie is contained in one cookiecontainer and appended to another httprequest request, session restoration can be implemented.
Some main code:
Cookiecontainer = new cookiecontainer ();
//////////////////////////////////////// ///////////
// 1. Open the login. ASPX page and obtain veiwstate & eventvalidation.
// If the login page is Asp.net, You need to obtain the veiwstate and eventvalidation
//////////////////////////////////////// ///////////
// Set parameters for opening the page
String uri = http: // localhost/test/login. aspx;
Httpwebrequest request = webrequest. Create (URI) as httpwebrequest;
Request. method = "get ";
Request. keepalive = false;
// Receive the returned page
Httpwebresponse response = request. getresponse () as httpwebresponse;
System. Io. Stream responsestream = response. getresponsestream ();
System. Io. streamreader reader = new system. Io. streamreader (responsestream, encoding. utf8 );
String srcstring = reader. readtoend ();
// Obtain the veiwstate of the page
String viewstateflag = "id =/" _ viewstate/"value = /"";
Int I = srcstring. indexof (viewstateflag) + viewstateflag. length;
Int J = srcstring. indexof ("/" ", I );
String viewstate = srcstring. substring (I, j-I );
// Obtain eventvalidation of the page
String eventvalidationflag = "id =/" _ eventvalidation/"value = /"";
I = srcstring. indexof (eventvalidationflag) + eventvalidationflag. length;
J = srcstring. indexof ("/" ", I );
String eventvalidation = srcstring. substring (I, j-I );
//////////////////////////////////////// ///////////
// 2. Automatically fill and submit the login. ASPX page
//////////////////////////////////////// ///////////
// Text of the submit button
String submitbutton = "Logon ";
// User name and password
String username = "1 ";
String Password = "1 ";
// Convert the text into a URL encoded string
Viewstate = system. Web. httputility. urlencode (viewstate );
Eventvalidation = system. Web. httputility. urlencode (eventvalidation );
Submitbutton = system. Web. httputility. urlencode (submitbutton );
// String data to be submitted. Format: User = uesr1 & Password = 123
String formatstring =
"Username = {0} & Password = {1} & loginbutton = {2} & __ viewstate = {3} & __ eventvalidation = {4 }";
String poststring =
String. Format (formatstring, username, password, submitbutton, viewstate, eventvalidation );
// Convert the submitted string data to a byte array
Byte [] postdata = encoding. ASCII. getbytes (poststring );
// Set parameters for submission
Request = webrequest. Create (URI) as httpwebrequest;
Request. method = "Post ";
Request. keepalive = false;
Request. contenttype = "application/X-WWW-form-urlencoded ";
Request. cookiecontainer = cookiecontainer;
Request. contentlength = postdata. length;
// Submit request data
System. Io. Stream outputstream = request. getrequeststream ();
Outputstream. Write (postdata, 0, postdata. Length );
Outputstream. Close ();
// Receive the returned page
Response = request. getresponse () as httpwebresponse;
Responsestream = response. getresponsestream ();
Reader = new system. Io. streamreader (responsestream, encoding. getencoding ("gb2312 "));
Srcstring = reader. readtoend ();
//////////////////////////////////////// ///////////
// 3. Open the default. aspx page
//////////////////////////////////////// ///////////
// Set parameters for opening the page
Uri = "http: // localhost: 1165/webtest/default. aspx ";
Request = webrequest. Create (URI) as httpwebrequest;
Request. method = "get ";
Request. keepalive = false;
Request. cookiecontainer = cookiecontainer;
// Receive the returned page
Response = request. getresponse () as httpwebresponse;
Responsestream = response. getresponsestream ();
Reader = new system. Io. streamreader (responsestream, encoding. utf8 );
Srcstring = reader. readtoend ();
//////////////////////////////////////// ///////////
// 4. Analyze the returned page
//////////////////////////////////////// ///////////
//