码迷,mamicode.com
首页 > 其他好文 > 详细

Cookie

时间:2014-05-29 03:41:11      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   java   

Cookie总是保存在客户端中,按在客户端中的存储位置,可分为内存Cookie和硬盘Cookie。

内存Cookie由浏览器维护,保存在内存中,浏览器关闭后就消失了,其存在时间是短暂的。硬盘Cookie保存在硬盘里,有一个过期时间,除非用户手工清理或到了过期时间,硬盘Cookie不会被删除,其存在时间是长期的。

Cookie的缺陷

  1. cookie会被附加在每个HTTP请求中,所以无形中增加了流量。
  2. 由于在HTTP请求中的cookie是明文传递的,所以安全性成问题。(除非用HTTPS)
  3. Cookie的大小限制在4KB左右。对于复杂的存储需求来说是不够用的。

ASP.NET C#读写Cookie

bubuko.com,布布扣
 1 /// <summary>
 2 /// 写cookie值
 3 /// </summary>
 4 /// <param name="strName">名称</param>
 5 /// <param name="strValue"></param>
 6 public static void WriteCookie(string strName, string strValue)
 7 {
 8     HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
 9     if (cookie == null)
10     {
11         cookie = new HttpCookie(strName);
12     }
13     cookie.Value = UrlEncode(strValue);
14     HttpContext.Current.Response.AppendCookie(cookie);
15 }
16 
17 /// <summary>
18 /// 写cookie值
19 /// </summary>
20 /// <param name="strName">名称</param>
21 /// <param name="strValue"></param>
22 /// <param name="expires">过期时间(分钟)</param>
23 public static void WriteCookie(string strName, string strValue, int expires)
24 {
25     HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
26     if (cookie == null)
27     {
28         cookie = new HttpCookie(strName);
29     }
30     cookie.Value = UrlEncode(strValue);
31     cookie.Expires = DateTime.Now.AddMinutes(expires);
32     HttpContext.Current.Response.AppendCookie(cookie);
33 }
34 
35 /// <summary>
36 /// 写cookie值
37 /// </summary>
38 /// <param name="strName">名称</param>
39 /// <param name="key"></param>
40 /// <param name="strValue"></param>
41 public static void WriteCookie(string strName, string key, string strValue)
42 {
43     HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
44     if (cookie == null)
45     {
46         cookie = new HttpCookie(strName);
47     }
48     cookie[key] = UrlEncode(strValue);
49     HttpContext.Current.Response.AppendCookie(cookie);
50 }
51 
52 /// <summary>
53 /// 写cookie值
54 /// </summary>
55 /// <param name="strName">名称</param>
56 /// <param name="key"></param>
57 /// <param name="strValue"></param>
58 /// <param name="expires">过期时间(分钟)</param>
59 public static void WriteCookie(string strName, string key, string strValue, int expires)
60 {
61     HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
62     if (cookie == null)
63     {
64         cookie = new HttpCookie(strName);
65     }
66     cookie[key] = UrlEncode(strValue);
67     cookie.Expires = DateTime.Now.AddMinutes(expires);
68     HttpContext.Current.Response.AppendCookie(cookie);
69 }
70 
71 /// <summary>
72 /// 读cookie值
73 /// </summary>
74 /// <param name="strName">名称</param>
75 /// <returns>cookie值</returns>
76 public static string GetCookie(string strName)
77 {
78     if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
79         return UrlDecode(HttpContext.Current.Request.Cookies[strName].Value.ToString());
80 
81     return "";
82 }
83 
84 /// <summary>
85 /// 读cookie值
86 /// </summary>
87 /// <param name="strName">名称</param>
88 /// <returns>cookie值</returns>
89 public static string GetCookie(string strName, string key)
90 {
91     if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null)
92         return UrlDecode(HttpContext.Current.Request.Cookies[strName][key].ToString());
93 
94     return "";
95 }
bubuko.com,布布扣

 

Cookie,布布扣,bubuko.com

Cookie

标签:style   c   class   blog   code   java   

原文地址:http://www.cnblogs.com/paullam/p/3754223.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!