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);
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("<br/>age" + cookie.Values.Count.ToString());
6 Response.Write("<br/>age" + cookie["name"]);
7 Response.Write("<br/>age" + cookie["age"]);
8 Response.Write("<br/>sex" + cookie.Values["sex"]);
9 Response.Write("<br/>sex" + cookie.Values[1]);
由于在服务器端我们不能直接删除cooike 所以只有设其过期2 if (null == cookie)
3 return;
4 Response.Write(cookie.Value);
5 Response.Write("<br/>age" + cookie.Values.Count.ToString());
6 Response.Write("<br/>age" + cookie["name"]);
7 Response.Write("<br/>age" + cookie["age"]);
8 Response.Write("<br/>sex" + cookie.Values["sex"]);
9 Response.Write("<br/>sex" + cookie.Values[1]);
//删除cookie
HttpCookie cookie2 = Request.Cookies["user"];
if (null == cookie2)
return;
cookie2.Expires = DateTime.Now.AddDays(-1);
Response.AppendCookie(cookie2);
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() - 存储Cookies</p>
126 </body>
127 </html>
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() - 存储Cookies</p>
126 </body>
127 </html>
转载无需注明出处,多多交流 ,欢迎访问我的博客:http://www.cnblogs.com/niuniu