Cookie is a piece of text information. Storing cookies on the client is one of the methods in which ASP. NET session States associate requests with sessions. Cookies can also be directly used to maintain data between requests, but the data will be stored on the client and sent to the server along with each request. The browser has a limit on the cookie size. Therefore, the cookie can be accepted only when the cookie size does not exceed 4096 bytes.
Compile cookie
// Method 1:
Response. Cookies ["username"]. value = "Mike ";
Response. Cookies ["username"]. expires = datetime. maxvalue;
// Method 2:
Httpcookie acookie = new httpcookie ("last ");
Acookie. value = "";
Acookie .. expires = datetime. maxvalue;
Response. Cookies. Add (acookie );
// Write multi-value cookies
// Method 1:
Response. Cookies ["userinfo1"] ["name"]. value = "Mike ";
Response. Cookies ["userinfo1"] ["last"]. value = "";
Response. Cookies ["userinfo1"]. expires = datetime. maxvalue;
// Method 2:
Httpcookie cookie = new httpcookie ("userinfo1 ");
Cookie. Values ["name"] = "Mike ";
Cookie. Values ["last"] = "";
Cookie. expires = datetime. maxvalue;
// Cookie. expires = system. datetime. Now. adddays (1); // set the expiration time to 1 day.
Response. Cookies. Add (cookie );
Read cookie
Internet Explorer saves site cookies in the file named <user >@< domain>. txt, where <user> is your account name.
Note: before obtaining the cookie value, make sure that the cookie exists. Otherwise, you will get an exception
If (request. Cookies ["username"]! = NULL)
{
String STR = request. Cookies ("username"). value;
}
// Read multi-value cookies
If (request. Cookies ["userinfo1"]! = NULL)
{
String name = request. Cookies ["userinfo1"] ["name"];
String last = request. Cookies ["userinfo1"] ["last"];
}
// Read the cookie set
For (INT I = 0; I <request. Cookies. Count; I ++)
{
Httpcookie cookies = request. Cookies;
Response. Write ("name =" + cookies. Mame + "<br/> ");
If (cookies. haskeys) // whether a subkey exists
{
System. Collections. Specialized. namevaluecollection namecoll
= Acookie. values;
For (Int J = 0; j <namecoll. Count; j ++)
{
Response. Write ("sub-key name =" + namecoll. allkey [J] + "<br/> ");
Response. Write ("sub-key value =" + namecoll [J] + "<br/> ");
}
}
Else
{
Response. Write ("value =" + cookies. Value + "<br/> ");
}
}
Run thisCodeYou can see a cookie named "ASP. net_sessionid". ASP. NET uses this cookie to save the unique identifier of your session.
Modify cookie
The modification method is the same as the creation method.
Delete cookie
Set its validity period to a previous date. When the browser checks the cookie validity period, the expired cookie is deleted.
Httpcookie cookie = new httpcookie ("userinfo1 ");
Cookie. expires = datetime. Now. adddays (-30 );
Response. Cookies. Add (cookie );
============================== Instance.
// ---------- Write cookie
Httpcookie cookie = new httpcookie ("userinfo1 ");
Cookie. expires = system. datetime. Now. addminutes (2); // set the expiration time
For (INT I = 0; I <20; I ++)
{
Cookie. Values ["Val" + I. tostring ()] = I. tostring ();
Response. Cookies. Add (cookie );
}
// Read the cookie set
For (INT I = 0; I <request. Cookies. Count; I ++)
{
If (request. Cookies. allkeys [I] = "userinfo1 ")
{
Httpcookie cookies = request. Cookies ["userinfo1"];
Response. Write ("name =" + cookies. Name + "<br/> ");
// Response. Write ("name =" + cookies. Value + "<br/> ");
If (cookies. haskeys) // whether a subkey exists
{
System. Collections. Specialized. namevaluecollection namecoll = cookies. values;
For (Int J = 0; j <namecoll. Count; j ++)
{
Response. Write ("subkey name =" + namecoll. allkeys [J] + "<br/> ");
Response. Write ("sub-key value =" + namecoll [J] + "<br/> ");
}
}< br> else
{< br> response. write ("value =" + cookies. value + "
");
}< BR >}< br>
}
/////******************************** **********************************
modify cookie
1 response. cookies ["info"] ["user"] = "2";
2 response. cookies ["info"]. expires = datetime. now. adddays (1); Delete the attributes under the cookie
1 httpcookie acookie = request. cookies ["info"];
2. acookie. values. remove ("userid");
3. acookie. expires = datetime. now. adddays (1);
4 response. cookies. add (acookie); Delete all cookies, that is, set the expiration time to now.
1 int Limit = request. cookies. count-1;
2 for (INT I = 0; I 3 {
4 acookie = request. cookies (I)
5. acookie. expires = datetime. now. adddays (-1)
6 response. cookies. add (acookie)
7} No need to search for it