多值Cookie結構及聲明
【主鍵(key)名稱】
【子健(key)名稱1】:【子健值】
【子健(key)名稱2】:【子健值】
【子健(key)名稱3】:【子健值】
聲明多值Cookie代碼如下(xià)
HttpCookie ck2=new HttpCookie(“multi_cookie”);
ck2.Values.Add(“name”,”小(xiǎo)王”);
ck2.Values.Add(“age”,”18”);
ck2.Values.Add(“sex”,”男”);
ck2.Expores=Date.Time.MaxValue;
Response.Cookies.Add(ck2);
讀取多值Cookie
HttpCookie _ck=Request.Cookies[“multi_cookie”];
string mystr=_ck.Values[“name”]+”,”+_ck.Values[“age”]+”,”+_ck.Values[“age”]+”歲”;
修改多值Cookie子健的值:
HttpCookie _ck=Request.Cookies[“multi_cookie”];
_ck.Values.Set(“KeyName”,”NewValue”);
_ck.Expores=Date.Time.MaxValue;
Response.Cookies.Add(_ck);
删除多值Cookie子健的值:
删除一(yī)個子健:_ck.Value.Remove(“KeyName”);
删除全部子健:_ck.Value.Clear();
Cookies的遍曆
1)、遍曆主鍵
foreach(string _key in Request.Cookies.AllKeys){...}
2)、遍曆子健
foreach(string _subkey in Request.Cookies.[_key].Values.keys){...}
具體(tǐ)代碼如下(xià)
string str_result=””;
foreach(string _key in Request.Cookies.AllKeys)//遍曆主鍵
{
HttpCookie _ck=Request.Cookies[_key]
foreach(_subkey in _ck.Values.keys )//遍曆子健
{
str_result+=_key+”|”+_subkey+”|”+_ck.Values[_subkey]+”\n”;
}
}