#region 读取或写入cookie 2 /// <summary> 3 /// 写cookie值 4 /// </summary> 5 /// <param name="strName">名称</param> 6 /// <param name="strValue">值</param> 7 public static void WriteCookie(string strName, string strValue) 8 { 9 HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; 10 if (cookie == null) 11 { 12 cookie = new HttpCookie(strName); 13 } 14 cookie.Value = UrlEncode(strValue); 15 HttpContext.Current.Response.AppendCookie(cookie); 16 } 17 18 /// <summary> 19 /// 写cookie值 20 /// </summary> 21 /// <param name="strName">名称</param> 22 /// <param name="strValue">值</param> 23 public static void WriteCookie(string strName, string key, string strValue) 24 { 25 HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; 26 if (cookie == null) 27 { 28 cookie = new HttpCookie(strName); 29 } 30 cookie[key] = UrlEncode(strValue); 31 HttpContext.Current.Response.AppendCookie(cookie); 32 } 33 34 /// <summary> 35 /// 写cookie值 36 /// </summary> 37 /// <param name="strName">名称</param> 38 /// <param name="strValue">值</param> 39 public static void WriteCookie(string strName, string key, string strValue, int expires) 40 { 41 HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; 42 if (cookie == null) 43 { 44 cookie = new HttpCookie(strName); 45 } 46 cookie[key] = UrlEncode(strValue); 47 cookie.Expires = DateTime.Now.AddMinutes(expires); 48 HttpContext.Current.Response.AppendCookie(cookie); 49 } 50 51 /// <summary> 52 /// 写cookie值 53 /// </summary> 54 /// <param name="strName">名称</param> 55 /// <param name="strValue">值</param> 56 /// <param name="strValue">过期时间(分钟)</param> 57 public static void WriteCookie(string strName, string strValue, int expires) 58 { 59 HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; 60 if (cookie == null) 61 { 62 cookie = new HttpCookie(strName); 63 } 64 cookie.Value = UrlEncode(strValue); 65 cookie.Expires = DateTime.Now.AddMinutes(expires); 66 HttpContext.Current.Response.AppendCookie(cookie); 67 } 68 /// <summary> 69 /// 写cookie值 70 /// </summary> 71 /// <param name="strName">名称</param> 72 /// <param name="expires">过期时间(天)</param> 73 public static void WriteCookie(string strName, int expires) 74 { 75 HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; 76 if (cookie == null) 77 { 78 cookie = new HttpCookie(strName); 79 } 80 cookie.Expires = DateTime.Now.AddDays(expires); 81 HttpContext.Current.Response.AppendCookie(cookie); 82 } 83 84 /// <summary> 85 /// 写入COOKIE,并指定过期时间 86 /// </summary> 87 /// <param name="strName">KEY</param> 88 /// <param name="strValue">VALUE</param> 89 /// <param name="expires">过期时间</param> 90 public static void iWriteCookie(string strName, string strValue, int expires) 91 { 92 HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; 93 if (cookie == null) 94 { 95 cookie = new HttpCookie(strName); 96 } 97 cookie.Value = strValue; 98 if (expires > 0) 99 { 100 cookie.Expires = DateTime.Now.AddMinutes((double)expires); 101 } 102 HttpContext.Current.Response.AppendCookie(cookie); 103 } 104 105 /// <summary> 106 /// 读cookie值 107 /// </summary> 108 /// <param name="strName">名称</param> 109 /// <returns>cookie值</returns> 110 public static string GetCookie(string strName) 111 { 112 if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null) 113 return UrlDecode(HttpContext.Current.Request.Cookies[strName].Value.ToString()); 114 return ""; 115 } 116 117 /// <summary> 118 /// 读cookie值 119 /// </summary> 120 /// <param name="strName">名称</param> 121 /// <returns>cookie值</returns> 122 public static string GetCookie(string strName, string key) 123 { 124 if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null) 125 return UrlDecode(HttpContext.Current.Request.Cookies[strName][key].ToString()); 126 127 return ""; 128 } 129 #endregion
时间: 2024-10-20 04:06:40