How to post information to a website and obtain cookiecontainer so that it can pass verification directly in the future

Source: Internet
Author: User
Code Using system;
Using system. net;
Using system. IO;
Using system. text;

Class Test
{
[Stathread]
Static void main (string [] ARGs)
{
//

// Todo: Add code here to start the application

//

String url = "http: // localhost/csdn2/1.asp ";

// <%

// If request ("AA") = "Zhuye" then Session ("OK") = "OK"

// If SESSION ("OK") = "OK" then

// Response. Write ("login ")

// Else

// Response. Write ("No Logon ")

// End if

// %>

String indata = "AA = Zhuye ";
String outdata = "";
Cookiecontainer mycookiecontainer = new cookiecontainer ();
// Create a cookiecontainer to store the cookie set

Httpwebrequest myhttpwebrequest = (httpwebrequest) webrequest. Create (URL );
// Create an httpwebrequest

Myhttpwebrequest. contenttype = "application/X-WWW-form-urlencoded ";
Myhttpwebrequest. contentlength = indata. length;
Myhttpwebrequest. method = "Post ";
Myhttpwebrequest. cookiecontainer = mycookiecontainer;
// Set cookiecontainer of httpwebrequest to the mycookiecontainer just created

Stream myrequeststream = myhttpwebrequest. getrequeststream ();
Streamwriter mystreamwriter = new streamwriter (myrequeststream, encoding. getencoding ("gb2312 "));
Mystreamwriter. Write (indata );
// Write data to the request stream of httpwebrequest

Mystreamwriter. Close ();
Myrequeststream. Close ();
// Close the open object

Httpwebresponse myhttpwebresponse = (httpwebresponse) myhttpwebrequest. getresponse ();
// Create an httpwebresponse

Myhttpwebresponse. Cookies = mycookiecontainer. getcookies (myhttpwebrequest. requesturi );
// Obtain the cookiecollection of a cookie set containing a URL

Stream myresponsestream = myhttpwebresponse. getresponsestream ();
Streamreader mystreamreader = new streamreader (myresponsestream, encoding. getencoding ("gb2312 "));
Outdata = mystreamreader. readtoend ();
// Read the data from the response stream of httpwebresponse

Mystreamreader. Close ();
Myresponsestream. Close ();
Console. writeline (outdata );
// Display "Logon"

// Get the cookie and then perform the request to directly read the Post-Logon content.

Myhttpwebrequest = (httpwebrequest) webrequest. Create (URL );
Myhttpwebrequest. cookiecontainer = mycookiecontainer ;//*

// The cookiecontainer that just now has a cookie, which can be directly verified by attaching it to httpwebrequest.

Myhttpwebresponse = (httpwebresponse) myhttpwebrequest. getresponse ();
Myhttpwebresponse. Cookies = mycookiecontainer. getcookies (myhttpwebrequest. requesturi );
Myresponsestream = myhttpwebresponse. getresponsestream ();
Mystreamreader = new streamreader (myresponsestream, encoding. getencoding ("gb2312 "));
Outdata = mystreamreader. readtoend ();
Mystreamreader. Close ();
Myresponsestream. Close ();
Console. writeline (outdata );
// Display "Logon" again"

// If you annotate the line *, it will display "No Logon"

}
}

The annotations are clearly written. Supplement: If you log on using the get method, you can directly modify the URL. indate can write nothing. (Do not modify myhttpwebrequest. method is get), for example, modifying the ASP file to If request. querystring ("AA") = "Zhuye" then Session ("OK") = "OK", you only need to change the URL to string url = "http: // localhost/csdn2/1.asp? AA = Zhuye.

 

 

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.