介绍完Cookie的基本原理后,下面简单描述下一次典型的网络浏览过程
客户端和服务器端对Cookie的操作
它们之间的交互我想通过下面的DEMO可以更好的让大家了解
a, 服务器端输出Cookie,客户端获取Cookie
protected void Page_Load(object sender, EventArgs e)

{
HttpCookie ck = new HttpCookie("TestCK");
ck.Values.Add("Name1", "1");
ck.Values.Add("Name2", "2");
HttpCookie ck1 = new HttpCookie("TestCK_2");
ck1.Value = "1";
this.Response.Cookies.Add(ck);
this.Response.Cookies.Add(ck1);
}
function GetCookie()
{
document.write(document.cookie);
}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,服务器端获取

function SetCookie()
{
document.cookie = "TestCK=Name1=1&Name2=2";
var d1 = new Date(2008, 9, 17);
document.cookie = "TestCK_2=1;expires=" + d1;
}执行完上面方法后,在客户端获取的Cookie如下图
需要注意的地方:
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)


/**//* Cookie类 */
function CookieObj(name)
{
/**//* (Public)名称 */
this.Name = name;
/**//* (Public)Cookie的键/值对 */
this.KeyValues = new Array();

/**//* (Private)document.cookie的字符串 */
this.__CookieValue;
}

CookieObj.prototype =
{

/**//* (Public)设置键/值对 */
Set : function(key, value)
{

switch(arguments.length)
{
case 0:
return;
break;
case 1:
if(!key)
{
return;
}
var item = this.GetItemByKey("__Default");
if(!item)
{
item = new KeyValuePair("__Default", key);
this.KeyValues.push(item);
}
else
{
item.Value = key;
}
break;
case 2:
// key或value为空 
if(!key || !value)
{
return;
}
var item = this.GetItemByKey(key);
//item为空的时候
if(!item)
{
item = new KeyValuePair(key, value);
this.KeyValues.push(item);
}
else
{
item.Value = value;
}
break;
}
},

/**//* (Public)删除键 */
Remove : function(key)
{
//key为空
if(!key)
{
return;
}
var index = this._GetIndexByKey(key);
//存在数据
if(index > -1)
{
this.KeyValues.splice(index, 1);
}
},

/**//* (Public)获取值 */
Get : function()
{
return this.GetItemByKey("__Default");
},

/**//* (Public)键/值对的索引 */
GetItemByKey : function(key)
{
//key为空
if(!key)
{
return;
}
//存在数据
if(this.KeyValues && this.KeyValues.length >0)
{
for(var i=0; i< this.KeyValues.length; i++)
{
var obj = this.KeyValues[i];
//关键字存在
if(obj.Key == key)
{
return obj;
break;
}
}
}
return null;
},

/**//* (Private)获取键/值对的Index */
_GetIndexByKey : function(key)
{
//存在数据
if(this.KeyValues && this.KeyValues.length >0)
{
for(var i=0; i< this.KeyValues.length; i++)
{
var obj = this.KeyValues[i];
//关键字存在
if(obj.Key == key)
{
return i;
break;
}
}
}
return -1;
}
}

/**//* 键/值对的类 */
function KeyValuePair(key, value)
{
this.Key = key;
this.Value = value;
}

/**//* Cookie和document.cookie之间的转换,获取,设置Cookie */
var CookieAdapter =
{};

/**//* (Public)获取所有的Cookie对象 */
CookieAdapter.GetCookies = function()
{
//Cookie对象的集合
var arrCookieObjs = new Array();
//Cookie存在
if(document.cookie)
{
var arrCookie = document.cookie.split(";");
for(var i=0;i < arrCookie.length; i++)
{
var mCookieObj = CookieAdapter._ConvertToCookieObj(arrCookie[i]);
arrCookieObjs.push(mCookieObj);
}
}
return arrCookieObjs;
}

/**//* (Public)获取指定名称的Cookie对象 */
CookieAdapter.GetCookieByName = function(name)
{
//Cookie存在
if(document.cookie)
{
var arrCookie = document.cookie.split(";");
for(var i=0;i < arrCookie.length; i++)
{
var arr = arrCookie[i].split("=");
if(arr[0] == name)
{
var mCookieObj = CookieAdapter._ConvertToCookieObj(arrCookie[i]);
return mCookieObj;
break;
}
}
}
return null;
}

/**//* (Public)设置document.cookie */
CookieAdapter.SetCookies = function(arrCookie, expires, domain, path, secure, httponly)
{
//Cookie对象不为空
if(arrCookie)
{
for(var i = 0; i< arrCookie.length; i++)
{
var obj = arrCookie[i];
var str = "";
//存在键/值集合
if(obj.KeyValues)
{
for(var j =0; j< obj.KeyValues.length; j++)
{
var objKey = obj.KeyValues[j];

if(objKey.Key == "__Default")
{
str += objKey.Value;
}
else
{
str += objKey.Key + "=" + objKey.Value;
}

if(j != obj.KeyValues.length -1)
{
str += "&";
}
}
obj.__CookieValue = str;
}

if(str)
{
document.cookie = obj.Name + "=" + str;
}
else
{
document.cookie = obj.Name;
}

if(expires)
{
document.cookie += ";expires=" + expires;
}
if(domain)
{
document.cookie += ";domain=" + domain;
}
if(path)
{
document.cookie += ";path=" + path;
}
if(secure)
{
document.cookie += ";" + secure;
}
if(httponly)
{
document.cookie += ";" + httponly;
}
}
}
}

/**//* (Private)document.cookie的转换为Cookie对象 */
CookieAdapter._ConvertToCookieObj = function(cookieStr)
{
var arr = cookieStr.split("=");
//设置Cookie对象
var mCookieObj = new CookieObj(arr[0]);

if(arr.length > 1)
{
var strValue = cookieStr.substring(arr[0].length + 1, cookieStr.length);
mCookieObj.__CookieValue = strValue;
//存在键/值集合
var arrValues = strValue.split("&");
for(var j=0 ;j < arrValues.length; j++)
{
var arrKeyValue = arrValues[j].split("=");

if(arrKeyValue.length == 1)
{
mCookieObj.Set(arrKeyValue[0]);
}
else
{
mCookieObj.Set(arrKeyValue[0], arrKeyValue[1]);
}
}
}
return mCookieObj;
}发表于 @ 2008年09月27日 10:23:00 | 评论( loading... ) | 举报| 收藏