出處:http://www.cnblogs.com/vebest/archive/2011/08/31/2161326.html
//寫入
protected void Button1_Click(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("MyCook");//初始化並設置Cookie的名稱
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(0, 0, 1, 0, 0);//過期時間為1分鐘
cookie.Expires = dt.Add(ts);//設置過期時間
cookie.Values.Add("userid", "userid_value");
cookie.Values.Add("userid2", "userid2_value2");
Response.AppendCookie(cookie);
//輸出該Cookie的所有內容
//Response.Write(cookie.Value);//輸出為:userid=userid_value&userid2=userid2_value2
}
//寫入
protected void Button1_Click(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("MyCook");//初始化並設置Cookie的名稱
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(0, 0, 1, 0, 0);//過期時間為1分鐘
cookie.Expires = dt.Add(ts);//設置過期時間
cookie.Values.Add("userid", "userid_value");
cookie.Values.Add("userid2", "userid2_value2");
Response.AppendCookie(cookie);
//輸出該Cookie的所有內容
//Response.Write(cookie.Value);//輸出為:userid=userid_value&userid2=userid2_value2
}
//讀取
protected void Button2_Click(object sender, EventArgs e)
{
protected void Button2_Click(object sender, EventArgs e)
{
// HttpCookie cokie = new HttpCookie("MyCook");//初始化
if (Request.Cookies["MyCook"] != null)
{
//Response.Write("Cookie中鍵值為userid的值:" + Request.Cookies["MyCook"]["userid"]);//整行
//Response.Write("Cookie中鍵值為userid2的值" + Request.Cookies["MyCook"]["userid2"]);
Response.Write(Request.Cookies["MyCook"].Value); //輸出全部的值
}
}
if (Request.Cookies["MyCook"] != null)
{
//Response.Write("Cookie中鍵值為userid的值:" + Request.Cookies["MyCook"]["userid"]);//整行
//Response.Write("Cookie中鍵值為userid2的值" + Request.Cookies["MyCook"]["userid2"]);
Response.Write(Request.Cookies["MyCook"].Value); //輸出全部的值
}
}
//修改Cookie
protected void Button3_Click(object sender, EventArgs e)
{
//獲取客戶端的Cookie對象
HttpCookie cok = Request.Cookies["MyCook"];
protected void Button3_Click(object sender, EventArgs e)
{
//獲取客戶端的Cookie對象
HttpCookie cok = Request.Cookies["MyCook"];
if (cok != null)
{
//修改Cookie的兩種方法
cok.Values["userid"] = "alter-value";
cok.Values.Set("userid", "alter-value");
{
//修改Cookie的兩種方法
cok.Values["userid"] = "alter-value";
cok.Values.Set("userid", "alter-value");
//往Cookie裡加入新的內容
cok.Values.Set("newid", "newValue");
Response.AppendCookie(cok);
}
}
cok.Values.Set("newid", "newValue");
Response.AppendCookie(cok);
}
}
//刪除Cookie
protected void Button4_Click(object sender, EventArgs e)
{
protected void Button4_Click(object sender, EventArgs e)
{
HttpCookie cok = Request.Cookies["MyCook"];
if (cok != null)
{
if (!true) //CheckBox1.Checked
{
cok.Values.Remove("userid"); //移除鍵值為userid的值
}
else
{
TimeSpan ts = new TimeSpan(-1, 0, 0, 0);
cok.Expires = DateTime.Now.Add(ts); //刪除整個Cookie,只要把過期時間設置為現在
}
Response.AppendCookie(cok);
}
}
if (cok != null)
{
if (!true) //CheckBox1.Checked
{
cok.Values.Remove("userid"); //移除鍵值為userid的值
}
else
{
TimeSpan ts = new TimeSpan(-1, 0, 0, 0);
cok.Expires = DateTime.Now.Add(ts); //刪除整個Cookie,只要把過期時間設置為現在
}
Response.AppendCookie(cok);
}
}
***************************************************************************************************************
HttpCookie addCookie = new HttpCookie("mycookie");
//添加cookie的key值
addCookie.Values.Add("name", "hsj");
//添加cookie的key值
addCookie.Values.Add("name", "hsj");
var cookie = Request.Cookies["mycookie"];
if (cookie != null && cookie.HasKeys)
{
//獲取cookie中key的兩種方法
var name1 = cookie["name"];
var name2 = cookie.Values["name"];
if (name1 != "hsj")
{
//設置cookie中key的兩種方法
cookie.Values["name"] = "zhangsan";
cookie.Values.Set("name", "lishi");
}
}
//更新cookie中現有的值
Response.SetCookie(cookie);
//將cookie添加到http respose中
Response.AppendCookie(addCookie);
if (cookie != null && cookie.HasKeys)
{
//獲取cookie中key的兩種方法
var name1 = cookie["name"];
var name2 = cookie.Values["name"];
if (name1 != "hsj")
{
//設置cookie中key的兩種方法
cookie.Values["name"] = "zhangsan";
cookie.Values.Set("name", "lishi");
}
}
//更新cookie中現有的值
Response.SetCookie(cookie);
//將cookie添加到http respose中
Response.AppendCookie(addCookie);
沒有留言:
張貼留言