if (!this.IsPostBack) { try { HttpCookie MyCookie = new HttpCookie("MyCookie"); MyCookie.Value = Server.HtmlEncode("我的Cookie应用程序");//自动把乱码转换成正常的字符串,如果直接赋值字符串就会出现乱码 MyCookie.Expires = DateTime.Now.AddDays(5); Response.Cookies.Add(MyCookie); Response.Write("Cookie创建成功"); HttpCookie GetCookie = Request.Cookies["MyCookie"]; Response.Write("Cookie的值:" + GetCookie.Value.ToString()+"<br/>"); Response.Write("Cookie的过期时间" + GetCookie.Expires.ToString()+"<br/>"); } catch { Response.Write("Cookie创建失败"); } }
时间: 2024-10-05 04:58:24