【精】COOKIE用法

分类:Javascript| 发布:camnprbubuol| 查看: | 发表时间:2011/6/14


Response对象和Request对象分别是包含HttpResponse和HttpRequest类实例的页面的属性
        Request.cookie        //将COOKIE发送给服务端

        Response.cookie       //将COOKIE保存在客户机

概念:yourCookieObj:对象实例名
            yourCookieName:对象名

一、创建COOKIE

        1.1、创建Cookie对象
                HttpCookie yourCookieObj= new HttpCookie("yourCookieName") ;  //定义cookie对象与名称
                yourCookieObj.Expires = DateTime.Now.AddDays(1) ;   //因为要有Expires的设置,才会存到客户机的硬盘上
                Response.Cookies.Add(yourCookieObj);       //确定写入客户机cookie中 
                另一种方法:不需要建立对象实例(在简单应用时更方便)
                       Response.Cookies["yourCookieName"].Expires = DateTime.Now.AddDays(1) ;  
                       Response.Cookies.Add(Response.Cookies["yourCookieName"]); 
                       因为:“Response.Cookies["yourCookieName"]”其实就是一个只由对象名表示(非实例化)的对象。

        1.2、给整个对象赋值
                1.2.1、yourCookieObj.Value = "Cookie对象内容";   
                1.2.2、Response.Cookies["yourCookieName"].Value = "Cookie对象内容";   
                1.2.3、也可以在创建对象时直接赋值
                            HttpCookie yourCookieObj= new HttpCookie("yourCookieName","Cookie对象内容") ;

        1.3、给子键赋值
                 1.3.1、yourCookieObj.Values["yourCookieKey1"] = "Key内容1";        //增加第一个键
                             yourCookieObj.Values["yourCookieKey2"] = "Key内容2";      //增加第三个键
                             yourCookieObj["yourCookieKey3"] = "Key内容3";    //增加第三个键。.Values可以省略掉

                 1.3.2、Response.Cookies["yourCookieName"]["yourCookieKey1"]="Key内容1"
                             Response.Cookies["yourCookieName"]["yourCookieKey2"]="Key内容2"

二:有效时间

        2.1、永久有效
                yourCookieObj.Expires = DateTime.MaxValue;
                或:yourCookieObj.Expires = DateTime.Now.AddYears(50) ;  

        2.2、定义具体过效时间
                2.2.1、yourCookieObj.Expires = DateTime.Now.Add年/天/时/分/秒() ;  

                2.2.1、DateTime dt = DateTime.Now;        //定义时间对象
                            TimeSpan ts=new TimeSpan(1,0,0,0);        //定义时间间隔,查msdn  (天,时,分,秒)
                            yourCookieObj.EXPires = dt.Add(ts);        //添加作用时间(时间对象Add时间间隔)

        2.3、如果没有设置 Cookie 的有效期,它不会保存到用户的硬盘上,
                而是会成为用户会话信息的一部分。如果用户关闭浏览器或会话超时,该 Cookie 就会被删除。

三:域

        3.1、yourCookieObj.Domain="域"
                这个很重要,当你在“aaa.qy21.com”建立COOKIE时,在“bbb.qy21.com”是无效的。
                如果我们在“aaa.qy21.com”设了yourCookieObj.Domain="qy21.com";
                        那么这个Cookie对象在“bbb.qy21.com”(也包括qy21.com的所有子域)都有效了!

四:读取

        4.2、HttpCookie yourCookieObj=Request.Cookies["yourCookieName"]; 
                                                                        //把客户机名为yourCookieName的Cookie对象赋给新建的对象实例

        4.1、yourCookieObj.属性   //属性如:Value、Values、Domain等等,只有Expires读不了
                 或:Request.Cookies["yourCookieName"].属性   //属性如:Value

        4.2、读取子建值

                4.2.1、string str1 = Request.Cookies["yourCookieName"]["yourCookieKey1"]; //读到的是:"Key内容1"
                            string str2 = Request.Cookies["yourCookieName"]["yourCookieKey2"];  //读到的是:"Key内容2"
                            string str3 = Request.Cookies["yourCookieName"].Value;
                                                                //  读到Cookie对象内容:"yourCookieKey1=内容1&yourCookieKey2=内容2"

                4.2.2、也可以先获取子键集合
                             System.Collections.Specialized.NameValueCollection
                                             yourCookieKeyValues = Request.Cookies["yourCookieName"].Values;
                            string str1 = yourCookieKeyValues ["yourCookieKey1"];
                            string str2 = yourCookieKeyValues ["yourCookieKey2"];

五:删除

        5.1、删除整个COOLIE对象
                yourCookieObj.Expires = DateTime.MinValue    //把时间设为过期时间就可以了
                或:yourCookieObj.Expires= DateTime.Now.AddDays(-1) ;

        5.2、删除某个子键
                 HttpCookie yourCookieObj= Request.Cookies["yourCookieName"];   
                 yourCookieObj.Values.Remove("yourCookieKey1");    //要删除的子键yourCookieKey1
                 yourCookieObj.Expires = DateTime.Now.AddDays(1) ;
                 Response.Cookies.Add(yourCookieObj);       //确定写入cookie中
 

六:修改与应用

        6.1、

        6.2、修改、追加、删除子键(假设原来已经有3个子键)
                HttpCookie yourCookieObj= Request.Cookies["yourCookieName"];
                yourCookieObj["yourCookieKey1"] = "Key内容111";    //修改子键
                yourCookieObj["yourCookieKey4"] = "Key内容4";    //添加子键
                yourCookieObj.Values.Remove("yourCookieKey3");   //删除子键
                yourCookieObj.Expires = DateTime.Now.AddDays(1);   
                Response.Cookies.Add(yourCookieObj);


其它:一、限制:

        1.1、浏览器限制每个域只能放20个Cookie对象

        1.2、一些浏览器限制所有域总共只能放300个Cookie对象

        1.3、每个Cookie对象的value值的大小限制在4K:
                经过IE测试:是限制在Cookie对象的Value字符串值为5054字节
                                        如果有“键(KEY)”时,连“键名”、“=”、“&”都算在5054个字节中。

                                        也就是可以放5054个纯英文或数字
                                        但汉字可能乱码,所以必须经过编码,不同的编码,占字节是不一样的:|
                                        *.1、HttpUtility.UrlEncode编码(由HttpUtility.UrlDecode解码)
                                                    每个汉字编码后是9个字节,所以实际上只能放561个汉字。
                                        *.2、HttpUtility.UrlEncodeUnicode编码(由HttpUtility.UrlDecode解码)
                                                    每个汉字编码后是6个字节,所以实际上只能放842个汉字。(推荐使用)
                                        *.3、HttpUtility.HtmlEncode 在某些方式查看还会乱码,不建议使用。

其它:二、汉字乱码

        2.1、通过HttpUtility.UrlEncodeUnicode编码,通过HttpUtility.UrlDecode解码
                如:写:Response.Cookies["yourCookieName"].Value = HttpUtility.UrlEncodeUnicode("内容");
                        读:HttpUtility.UrlDecode(Request.Cookies["yourCookieName"].Value)

其它:三、注意:

        3.1、给COOKIE赋值时,应该给“=”、“&”进行语义转化

引自:http://blog.csdn.net/wcg83/archive/2008/08/06/2777390.aspx
365据说看到好文章不转的人,服务器容易宕机
原创文章如转载,请注明:转载自郑州网建-前端开发 http://camnpr.com/
本文链接:http://camnpr.com/archives/349.html