The Asp.net program automatically submits the login form session and cookie

Source: Internet
Author: User

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
//////////////////////////////////////// ///////////
//

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.