在WEB页中设置COOKIES
一设置cookies的方法很简单有以下两种方法
直接添加Cookie值
ResponseCookies[userName] = Tom;
ResponseCookies[userName]Expires = DateTimeNowAddDays() ; \\过期时间在Cookies文件中无法查看也不能调用
创建Cookie对象的一个实例
HttpCookie cookie=new HttpCookie(userName);
cookieValue = Tom;
cookieExpires = DateTimeNowAddDays() ;
ResponseCookiesAdd(aCookie)
用以上任一方法都可以生成一个有userName项的文件 在你的Internet临时文件夹中你可以查看它
也可以创建和添加有子键的Cookies如
ResponseCookies[userInfo][userName] = Tom;
或
HttpCookie cookie=new HttpCookie(userInfo);
cookieValues[userName] = Tom;
aCookieExpires = DateTimeNowAddDays();
ResponseCookiesAdd(aCookie)
二检索Cookies:
Cookies某一键的值为
ServerHtmlEncode(RequestCookies[userInfo][userName])
你可以用ResponseWrite()方法输出它到页面如
ResponseWrite(ServerHtmlEncode(RequestCookies[userInfo][userName]))
或赋值给其它变量
string strCookie=ServerHtmlEncode(RequestCookies[userInfo][userName]);
用Cookies[i]数组可以检索所有项和子键如
string[] cooName = new string[RequestCookiesCount];
string[] cooValue = new string[RequestCookiesCount];
HttpCookie aCookie;
for(int i=;i<RequestCookiesCount;i++){
aCookie = RequestCookies[i];
cooName[i] = ServerHtmlEncode(aCookieName);
if(!aCookieHasKeys){
cooValue[i] = ServerHtmlEncode(aCookieValue);
}else{
string[] subcooName = new string[aCookieValuesCount];
string[] subcooValue = new string[aCookieValuesCount];
for(int j=;j<aCookieValuesCount;j++){
subcooName[j] = ServerHtmlEncode(aCookieValuesAllKeys[j]);
subcooValue[j] = ServerHtmlEncode(aCookieValues[j]);
}
}
}
三修改Cookies
如果是数值类型的Cookie值比如访问次数你可以读取该值进行加减操作后再存回一般的修改直接存入新值就可以了系统自动用新值覆盖原值存入的方法与创建相同
四删除Cookies
删除Cookies只要把有效期设为失效就可以了如在创建时设有效期为一天
cookieExpires = DateTimeNowAddDays() ;
要删除则设为
cookieExpires = DateTimeNowAddDays() ;
删除子键
HttpCookie cookie;
cookie = RequestCookies[userInfo];
aCookieValuesRemove(userName);
aCookieExpires = DateTimeNowAddDays();
ResponseCookiesAdd(aCookie);