(Forum FAQ) If you post information to a Web site and get Cookiecontainer so that you can pass validation directly later
Source: Internet
Author: User
Cookies using System;
Using System.Net;
Using System.IO;
Using System.Text; [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 login")
End If
%>
String Indata= "Aa=zhuye";
String Outdata= "";
Cookiecontainer mycookiecontainer=new Cookiecontainer ();
Create a new Cookiecontainer to store the cookie collection
HttpWebRequest myhttpwebrequest= (HttpWebRequest) webrequest.create (URL);
Create a new HttpWebRequest
Myhttpwebrequest.contenttype= "application/x-www-form-urlencoded";
Myhttpwebrequest.contentlength=indata. Length;
Myhttpwebrequest.method= "POST";
Myhttpwebrequest.cookiecontainer=mycookiecontainer;
Set the HttpWebRequest Cookiecontainer for the Mycookiecontainer that was just built.
Stream Myrequeststream=myhttpwebrequest.getrequeststream ();
StreamWriter Mystreamwriter=new StreamWriter (myrequeststream,encoding.getencoding ("gb2312"));
Mystreamwriter.write (Indata);
Write the data to the HttpWebRequest request stream
Mystreamwriter.close ();
Myrequeststream.close ();
Close Open Object
HttpWebResponse myhttpwebresponse= (HttpWebResponse) myhttpwebrequest.getresponse ();
Create a new HttpWebResponse
Myhttpwebresponse.cookies=mycookiecontainer.getcookies (Myhttpwebrequest.requesturi);
Gets a cookiecollection of the cookie collection that contains the URL
Stream Myresponsestream=myhttpwebresponse.getresponsestream ();
StreamReader mystreamreader=new StreamReader (myresponsestream,encoding.getencoding ("gb2312"));
Outdata=mystreamreader.readtoend ();
Read the data from the HttpWebResponse response stream
Mystreamreader.close ();
Myresponsestream.close ();
Console.WriteLine (Outdata);
Display "Login"
Get a cookie, and then make a request to read directly to the content after the login
Myhttpwebrequest= (HttpWebRequest) webrequest.create (URL);
myhttpwebrequest.cookiecontainer=mycookiecontainer;//*
The Cookiecontainer already has a cookie, attaching it to the HttpWebRequest can be validated directly
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);
Show "Login" again
If the * line comment is adjusted, it shows "no login"
}
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.