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.
The complete code is as 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;