學習心得:asp.net操作cookie

iDotNetSpace發表於2009-07-24
   HttpCookie cookie1 = new HttpCookie("user""liuliang");
        cookie1[
"age"= "18";
        cookie1.Values.Add(
"sex""");
        cookie1.Expires 
= DateTime.Now.AddHours(1);
        Response.AppendCookie(cookie1);

        HttpCookie cookie 
= new HttpCookie("luis");
        cookie.Values.Add(
"name""dangdang");
        cookie[
"age"= "23";
        cookie[
"sex"= "man";
        cookie.Expires 
= DateTime.Now.AddMinutes(0.5);
        Response.Cookies.Add(cookie);

然後讀取:

1         HttpCookie cookie = Request.Cookies["luis"];
2         if (null == cookie)
3             return;
4         Response.Write(cookie.Value);
5         Response.Write("
age
" + cookie.Values.Count.ToString());
6         Response.Write("
age
" + cookie["name"]);
7         Response.Write("
age
" + cookie["age"]);
8         Response.Write("
sex
" + cookie.Values["sex"]);
9         Response.Write("
sex
" + cookie.Values[1]);
由於在伺服器端我們不能直接刪除cooike 所以只有設其過期
        //刪除cookie
        HttpCookie cookie2 = Request.Cookies["user"];
        
if (null == cookie2)
            
return;
        cookie2.Expires 
= DateTime.Now.AddDays(-1);
        Response.AppendCookie(cookie2);
呵呵 就這麼簡單
然後我們可以:
進行客戶端來顯示cookie
最簡單的程式碼是:
var c=document.cookie.toString();
alert(c);
還可以用cookie類操作:
下面是轉自某高人的操作類:
  1 DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2 
  3 <html xmlns="http://www.w3.org/1999/xhtml">
  4 <head>
  5     <title>無標題頁title>
  6 head>
  7 <body>
  8 <script language="JavaScript" type="text/javascript">
  9 function Cookie(delim){
 10 //操作Cookie類
 11 Array.prototype.splice=function(){
 12 var len=arguments.length,tarray=[],i=arguments[0]+arguments[1];
 13 if(len>1){
 14 while(i<this.length)tarray[tarray.length]=this[i++];
 15 this.length=arguments[0];
 16 if(len>2)for(var i=2;i<len;i++)this[this.length]=arguments[i];
 17 var tlen=tarray.length,i=0;
 18 while(i<tlen)this[this.length]=tarray[i++];
 19 }
 20 return this;
 21 }
 22 this._Cookie=[];
 23 this.Load=function(){
 24 if(document.cookie.indexOf(";")!=-1){
 25 var _sp,_name,_tp,_tars,_tarslength;
 26 var _item=document.cookie.split("");
 27 var _itemlength=_item.length;
 28 while(_itemlength>0){
 29 _sp=_item[--_itemlength].split("=");
 30 _name=_sp[0];
 31 _tp=_sp[1].split(",");
 32 _tars=_tp.slice(1,_tp.length);
 33 this._Cookie[_name]=[];
 34 this._Cookie[_name]=_tars;
 35 this._Cookie[_name]["timeout"]=_tp[0];
 36 }
 37 return true;
 38 }
 39 return false;
 40 }
 41 this.Save=function(){
 42 var _str,_ars,_mars,_marslength,timeout,i,key;
 43 for(key in this._Cookie){
 44 if(!this._Cookie[key])return;
 45 _str=[];
 46 _mars=CookieClass._Cookie[key];
 47 _marslength=_mars.length;
 48 for(i=0;i<_marslength;i++)_str[_str.length]=escape(_mars[i]);
 49 document.cookie=key+"="+_mars["timeout"]+(_str.length>0?",":"")+_str+";expires="+new Date(parseInt(_mars["timeout"])).toGMTString();
 50 }
 51 
 52 }
 53 this.GetCookieCount=function(){
 54 var _length=0,key;
 55 for(key in this._Cookie)_length++;
 56 return _length;
 57 }
 58 this.Create=function(name,days){
 59 this._Cookie[name]=[];
 60 this._Cookie[name]["timeout"]=new Date().getTime()+days*86400000;
 61 }
 62 this.Modify=function(name,days){
 63 this.Create(name,days);
 64 }
 65 this.GetTime=function(name){
 66 return new Date(parseInt(this._Cookie[name]["timeout"]));
 67 }
 68 this.Delete=function(name){
 69 this.Create(name,0);
 70 }
 71 this.AddItem=function(name,value){
 72 this._Cookie[name][this._Cookie[name].length]=value;
 73 }
 74 this.DelItem=function(name,index){
 75 this._Cookie[name].splice(index,1);
 76 }
 77 this.GetCount=function(name){
 78 return this._Cookie[name].length;
 79 }
 80 this.GetItem=function(name,index){
 81 return this._Cookie[name][index];
 82 }
 83 }
 84 script>
 85 <script type="text/javascript" language="javascript">
 86 //返回cookie數量
 87 var CookieClass=new Cookie();
 88 if(!CookieClass.Load()){
 89 CookieClass.Create("Pass",1);
 90 CookieClass.Create("ll",1);
 91 CookieClass.AddItem("Pass","Ps1");
 92 CookieClass.AddItem("Pass","Ps2");
 93 CookieClass.AddItem("Pass","Ps3");
 94 CookieClass.AddItem("Pass","Ps4");
 95 CookieClass.AddItem("Pass","Ps5");
 96 CookieClass.AddItem("Pass","Ps6");
 97 CookieClass.DelItem("Pass",1);
 98 CookieClass.Save();
 99 }
100 alert("Cookie過期時間:"+CookieClass.GetTime("Pass").toLocaleString());
101 alert(document.cookie);
102 
103 script>
104     <p>
105         Cookies物件的使用<br />
106         以下是物件的公有方法:<br />
107         <br />
108         方括號[]內是可選引數<br />
109         //構造<br />
110         Cookie([定界符,預設為句點]) - 建構函式<br />
111         <br />
112         //初始化<br />
113         GetCookieCount() - 返回Cookies數量<br />
114         Create(name, days) - 建立Cookies及其時效天數<br />
115         Modify(name, days) - 修改Cookies的時效天數<br />
116         Delete(name) - 刪除Cookies及其子項<br />
117         GetTime(name) - 返回指定Cookies的過期時間<br />
118         GetCount(name) - 返回Cookies的子項數量<br />
119         AddItem(name,value) - 增加一個子項<br />
120         GetItem(name,index) - 返回指定索引的子項<br />
121         DelItem(name,index) - 刪除指定的子項<br />
122         <br />
123         //存取<br />
124         Load() - 讀取Cookies<br />
125         Save() - 儲存Cookiesp>
126 body>
127 html>

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-610379/,如需轉載,請註明出處,否則將追究法律責任。

相關文章