首页 > 代码库 > Cookies操作
Cookies操作
1. http Cookies赋值:
HttpCookie cookies;
cookies = new HttpCookie("Fanfenghua");
cookies.Values.Add("Name", "Fanfenghua"); //添加值
cookies.Values.Add("Age", "30");
cookies["Sex"] = "Man"; //另一种赋值模式
DateTime dt = DateTime.Now.AddHours(20);
cookies.Expires = dt; //失效时间
Response.AppendCookie(cookies);
2. http cookies取值 :
string name="";
string age="";
HttpCookie cookies = Request.Cookies["Fanfenghua"];
if (cookies != null)
{
name = cookies["Name"];
age = cookies["Age"];
}
Label1.Text = name;
Label2.Text = age;
3.删除Cookie:
HttpCookie cookie = Request.Cookies["Fanfenghua"];
if (cookie != null)
{
DateTime dt = DateTime.Now.AddDays(-1);
cookie.Expires = dt;
Response.AppendCookie(cookie);
}