Cookie用法
//寫入
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)
{
//?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);//輸出全部的值
}
}
//修改Cookie
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.Set("newid",?"newValue");
Response.AppendCookie(cok);
}??????
}
//刪除Cookie
protected?void?Button4_Click(object?sender,?EventArgs?e)
{
HttpCookie?cok?=?Request.Cookies["MyCook"];
if?(cok?!=?null)
{
if?(!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);
}
}
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)
{
//?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);//輸出全部的值
}
}
//修改Cookie
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.Set("newid",?"newValue");
Response.AppendCookie(cok);
}??????
}
//刪除Cookie
protected?void?Button4_Click(object?sender,?EventArgs?e)
{
HttpCookie?cok?=?Request.Cookies["MyCook"];
if?(cok?!=?null)
{
if?(!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);
}
}
轉載于:https://www.cnblogs.com/manwwx129/p/7098836.html
總結
- 上一篇: 修改CentOS 7.2系统的主机名
- 下一篇: 08_传智播客iOS视频教程_Found