对 cookies 操作!

来源:互联网 发布:网络人netman 编辑:程序博客网 时间:2024/05/20 21:47

#中cookies的读取写入操作如下:

/////////////////////////////读取///////////////////////////////

//获得此cookie对象

HttpCookie cookie = Request.Cookies["demo"];
//检验Cookie是否已经存在
if (null == cookie) {
Response.Write("Cookie not found. <br><hr>");
}
else {
//显示Cookie的值
String strCookieValue = cookie.Value.ToString();
Response.Write("The " + strCookieName + " cookie contains: <b>"
+ strCookieValue + "</b><br><hr>");
}

/////////////////////////////写入///////////////////////////////

//创建一个新Cookie
HttpCookie cookie = new HttpCookie("demo");
//设定Cookie的值
cookie.Value = "value";
//设定cookie生命为1周,也就是7天
cookie.Expires = DateTime.Now.AddDays(7);
//添加Cookie
Response.Cookies.Add(cookie);

原创粉丝点击