When using cookies today, you may encounter some problems:
One is that the cookie always displays garbled characters when reading the content: I have never understood why it is like this. Later I want to use the server. urlencode encodes the content and decodes it when it is displayed:
The following is a simple cookie written by myself.
CompleteCodeAs follows:
Protected void button2_click (Object sender, eventargs E)
{
String cookiename = "123 ";
Httpcookie cookie = new httpcookie (cookiename );
Cookie. value = server. urlencode ("Han Guizhou ");
Datetime dtnow = datetime. now;
Timespan tsminute = new timespan (0, 1, 0, 0 );
Cookie. expires = dtnow + tsminute;
Response. Cookies. Add (cookie );
Label1.text = cookie. value;
}
Protected void button#click (Object sender, eventargs E)
{
Httpcookie getcookie = request. Cookies ["123"];
If (Null = getcookie)
{
Response. Write ("corresponding cookie not found ");
}
Else
{
Label1.text = server. urldecode (getcookie. value. tostring ());
}
}
In addition, it should be noted that the method to determine whether the cookie content exists is
If (null = cookie name)
Third, pay attention to the cookie survival time:
In this example. Its survival time is one hour.
Datetime dtnow = datetime. now;
Timespan tsminute = new timespan (0, 1, 0, 0 );
Cookie. expires = dtnow + tsminute;