|
Posted on
2008-09-18 13:10
Kevin-moon
阅读( 6811)
评论()
编辑
收藏
举报
Cookie这个东东,第一次接触WEB的时候,就了解它了,用起来嘛也很简单.但是对这个东西一直缺少完整的认识,最近正好有时间,看了些资料,结合自己以前写的代码,在这里做个简单的完整介绍吧.
基本概念
Cookie是Web服务器向用户浏览器发送的一段Ascii文本.一旦接受到cookie,浏览器会把cookie的信息片段以"键/值"对的形式保存在本地.这以后,每次想同一服务器发送请求的时候,Web浏览器都会发送站点以前存储在本地的cookie.浏览器和Web服务器的通讯是通过Http协议进行通讯的,而cookie就保存在Http协议的请求部分(Set-Cookie).
具体形式如下:
Set-Cookie:customer=huangxp; path=/foo; domain=ibm.com; expires= Wednesday, 19-OCT-05 23:12:40 GMT; [secure];HttpOnly
其中每个属性的解释:
domain: 关联的域名,例如http://ibm.com/foo/index.aspx, 它的domain = ibm.com,该domain默认为当前请求的域,但是如果cookie中domain的值和请求的域不相符的话,这个cookie就会被忽略.
path: 控制哪些访问能触发发送.例如请求的地址是上面的URL,如果path=/foo,这个cookie就会被发送,但是path为其他的话,该cookie会被忽略.
expires: cookie的过期时间
secure: 如果secure 这个词被作为Set-Cookie 头的一部分,那么cookie 只能通过安全通道传输(目前即SSL通道)。否则,浏览器将忽略此Cookie
HttpOnly:只是该cookie是否能被客户端访问,不过该数据要依赖与浏览器是否支持,一般IE6以上的版本都支持该属性.
"键/值"对: customer=huangxp或customer=a1=huangxp&a2=huangxp
介绍完Cookie的基本原理后,下面简单描述下一次典型的网络浏览过程
- 浏览器对于Web服务器应答包头中Cookie的操作步骤:
1. 从Web服务器的应答包头中提取所有的cookie。
2. 解析这些cookie的组成部分(名称,值,路径等等)。
3. 判定主机是否允许设置这些cookie。允许的话,则把这些Cookie存储在本地。
- 浏览器对Web服务器请求包头中所有的Cookie进行筛选的步骤:
1. 根据请求的URL和本地存储cookie的属性,判断那些Cookie能被发送给Web服务器。
2. 对于多个cookie,判定发送的顺序。
3. 把需要发送的Cookie加入到请求HTTP包头中一起发送。
客户端和服务器端对Cookie的操作
通过上述介绍,大家对Cookie的已经有了完整的认识了吧,下面介绍下Cookie的操作:
1,服务器端的操作
HttpCookie是Cookie的类,对于它,大家应该很了解吧,不了解的可以去查下MSDN上.对于Cookie的获取和输出,通过HttpRequest和HttpResponse来实现的.
2,客户端操作
document.cookie,通过这个对象来获取和设置Cookie的.
它们之间的交互我想通过下面的DEMO可以更好的让大家了解
a, 服务器端输出Cookie,客户端获取Cookie
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) Code
protected void Page_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) {
HttpCookie ck = new HttpCookie("TestCK");
ck.Values.Add("Name1", "1");
ck.Values.Add("Name2", "2");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
HttpCookie ck1 = new HttpCookie("TestCK_2");
ck1.Value = "1";
this.Response.Cookies.Add(ck);
this.Response.Cookies.Add(ck1);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) Code
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) function GetCookie() {
document.write(document.cookie);
}
服务器端输出TestCk,TestCK_2这两个Cookie,那么客户段获取的Cookie是"TestCK=Name1=1&Name2=2; TestCK_2=1"
有些需要注意的地方:
1,客户端的document.cookie只能获取获取HttpCookie的Name,Value和Values属性.
2,如果HttpCookie中的Values有值的话,那么在客户端输出的是name1=1&name2=2&......这种形式,如果Value有值,输出的为1,如果Value和Values都有值,输出的是1&name1=1&name2=2&...这种形式
3,多个Cookie在客户端document.cookie中是通过";"来隔离的
b, 客户端输出Cookie,服务器端获取
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) Code
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) function SetCookie() {
document.cookie = "TestCK=Name1=1&Name2=2";
var d1 = new Date(2008, 9, 17);
document.cookie = "TestCK_2=1;expires=" + d1;
}
执行完上面方法后,在客户端获取的Cookie如下图
![](https://images.cnblogs.com/cnblogs_com/kevin-moon/Cookie.GIF)
服务器端获取的Cookie:
![](https://images.cnblogs.com/cnblogs_com/kevin-moon/ServerCookie.GIF)
需要注意的地方:
1, 设置多个Cookie的时候必须按照上面的设置,不能"TestCK=Name1=1&Name2=2; TestCK_2=1"这样赋值
2, 如果要删除Cookie,可以设置expires属性为过期的时间,例如"document.cookie = TestCK_2=1;expires = 过期时间"
3, 对于在客户端设置的expires..这些属性,在服务器端获取不到,只能获取Value和Values属性(至于为什么会这样我也没有弄明白?)
不管在服务器端和客户端都要注意对domain,path,httponly.... 这些的设置,在没有特殊需求的时候,别去设置他们,否则可能会造成Cookie遗失.
自定义Cookie类(客户端操作Cookie)
根据document.cookie对Cookie的支持,在赋值和获取值的操作上还是有点麻烦,不像服务器端的HttpCookie那样方便,则下面提供了个自己写的在客户端操作Cookie的对象.基本和HttpCookie相对应,目的是为了操作起来更加方便一些.
CookieObj类: 对应与HttpCookie的Name和Values, __CookieValue为私有属性,一般不要使用,它的值为当前Name对应的document.cookie.
Set方法: 为当前Cookie赋值,设置Values属性.
Remove方法: 根据Key删除Values中的元素.
Get方法: 获取默认的值,因为document.cookie的值可能为"1&name1=1&name2=2"这种形式,所以它获取的是1的元素
GetItemByKey方法: 根据Key获取元素
KeyValuePair类: 这个只是一个键/值对的类.
CookieAdapter类: 提供一种document.cookie和CookieObj之间的转换
CookieAdapter.GetCookies方法 : 获取所有的Cookie,并且转换为CookieObj的数组集合.
CookieAdapter.GetCookieByName方法: 根据Cookie的名称,来获取对应的Cookie.
CookieAdapter.SetCookies方法: 设置document.cookie,接受的参数是由CookieObj对象组成的数组集合.
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) Code
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//* Cookie类 */
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) function CookieObj(name) {
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//* (Public)名称 */
this.Name = name;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//* (Public)Cookie的键/值对 */
this.KeyValues = new Array();
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//* (Private)document.cookie的字符串 */
this.__CookieValue;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) CookieObj.prototype = {
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//* (Public)设置键/值对 */
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) Set : function(key, value) {
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) switch(arguments.length) {
case 0:
return;
break;
case 1:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(!key) {
return;
}
var item = this.GetItemByKey("__Default");
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(!item) {
item = new KeyValuePair("__Default", key);
this.KeyValues.push(item);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) else {
item.Value = key;
}
break;
case 2:
// key或value为空
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(!key || !value) {
return;
}
var item = this.GetItemByKey(key);
//item为空的时候
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(!item) {
item = new KeyValuePair(key, value);
this.KeyValues.push(item);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) else {
item.Value = value;
}
break;
}
},
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//* (Public)删除键 */
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) Remove : function(key) {
//key为空
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(!key) {
return;
}
var index = this._GetIndexByKey(key);
//存在数据
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(index > -1) {
this.KeyValues.splice(index, 1);
}
},
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//* (Public)获取值 */
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) Get : function() {
return this.GetItemByKey("__Default");
},
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//* (Public)键/值对的索引 */
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) GetItemByKey : function(key) {
//key为空
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(!key) {
return;
}
//存在数据
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(this.KeyValues && this.KeyValues.length >0) {
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) for(var i=0; i< this.KeyValues.length; i++) {
var obj = this.KeyValues[i];
//关键字存在
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(obj.Key == key) {
return obj;
break;
}
}
}
return null;
},
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//* (Private)获取键/值对的Index */
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) _GetIndexByKey : function(key) {
//存在数据
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(this.KeyValues && this.KeyValues.length >0) {
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) for(var i=0; i< this.KeyValues.length; i++) {
var obj = this.KeyValues[i];
//关键字存在
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(obj.Key == key) {
return i;
break;
}
}
}
return -1;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//* 键/值对的类 */
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) function KeyValuePair(key, value) {
this.Key = key;
this.Value = value;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//* Cookie和document.cookie之间的转换,获取,设置Cookie */
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) var CookieAdapter = {};
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//* (Public)获取所有的Cookie对象 */
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) CookieAdapter.GetCookies = function() {
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Cookie对象的集合
var arrCookieObjs = new Array();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Cookie存在
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(document.cookie) {
var arrCookie = document.cookie.split(";");
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) for(var i=0;i < arrCookie.length; i++) {
var mCookieObj = CookieAdapter._ConvertToCookieObj(arrCookie[i]);
arrCookieObjs.push(mCookieObj);
}
}
return arrCookieObjs;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//* (Public)获取指定名称的Cookie对象 */
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) CookieAdapter.GetCookieByName = function(name) {
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Cookie存在
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(document.cookie) {
var arrCookie = document.cookie.split(";");
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) for(var i=0;i < arrCookie.length; i++) {
var arr = arrCookie[i].split("=");
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(arr[0] == name) {
var mCookieObj = CookieAdapter._ConvertToCookieObj(arrCookie[i]);
return mCookieObj;
break;
}
}
}
return null;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//* (Public)设置document.cookie */
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) CookieAdapter.SetCookies = function(arrCookie, expires, domain, path, secure, httponly) {
//Cookie对象不为空
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(arrCookie) {
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) for(var i = 0; i< arrCookie.length; i++) {
var obj = arrCookie[i];
var str = "";
//存在键/值集合
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(obj.KeyValues) {
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) for(var j =0; j< obj.KeyValues.length; j++) {
var objKey = obj.KeyValues[j];
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(objKey.Key == "__Default") {
str += objKey.Value;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) else {
str += objKey.Key + "=" + objKey.Value;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(j != obj.KeyValues.length -1) {
str += "&";
}
}
obj.__CookieValue = str;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(str) {
document.cookie = obj.Name + "=" + str;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) else {
document.cookie = obj.Name;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(expires) {
document.cookie += ";expires=" + expires;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(domain) {
document.cookie += ";domain=" + domain;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(path) {
document.cookie += ";path=" + path;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(secure) {
document.cookie += ";" + secure;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(httponly) {
document.cookie += ";" + httponly;
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//* (Private)document.cookie的转换为Cookie对象 */
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) CookieAdapter._ConvertToCookieObj = function(cookieStr) {
var arr = cookieStr.split("=");
//设置Cookie对象
var mCookieObj = new CookieObj(arr[0]);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(arr.length > 1) {
var strValue = cookieStr.substring(arr[0].length + 1, cookieStr.length);
mCookieObj.__CookieValue = strValue;
//存在键/值集合
var arrValues = strValue.split("&");
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) for(var j=0 ;j < arrValues.length; j++) {
var arrKeyValue = arrValues[j].split("=");
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(arrKeyValue.length == 1) {
mCookieObj.Set(arrKeyValue[0]);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) else {
mCookieObj.Set(arrKeyValue[0], arrKeyValue[1]);
}
}
}
return mCookieObj;
}
|