时间:2021-07-01 10:21:17 帮助过:8人阅读
string[] cooName = new string[Request.Cookies.Count];
string[] cooValue = new string[Request.Cookies.Count];
HttpCookie aCookie;
for(int i=0;i<Request.Cookies.Count;i++){
aCookie = Request.Cookies[i];
cooName[i] = Server.HtmlEncode(aCookie.Name);
if(!aCookie.HasKeys){
cooValue[i] = Server.HtmlEncode(aCookie.Value);
}else{
string[] subcooName = new string[aCookie.Values.Count];
string[] subcooValue = new string[aCookie.Values.Count];
for(int j=0;j<aCookie.Values.Count;j++){
subcooName[j] = Server.HtmlEncode(aCookie.Values.AllKeys[j]);
subcooValue[j] = Server.HtmlEncode(aCookie.Values[j]);
}
}
}
三、修改Cookies
如果是数值类型的Cookie值,比如访问次数,你可以读取该值进行加减操作后再存回,一般的修改直接存入新值就可以了,系统自动用新值覆盖原值,存入的方法与创建相同。
四、删除Cookies
删除Cookies只要把有效期设为失效就可以了,如在创建时设有效期为一天:
cookie.Expires = DateTime.Now.AddDays(1) ;
要删除则设为:
cookie.Expires = DateTime.Now.AddDays(-1) ;
删除子键:
代码如下:
HttpCookie cookie;
cookie = Request.Cookies["userInfo"];
aCookie.Values.Remove("userName");
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);