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.