zoukankan      html  css  js  c++  java
  • Cookie帮助类

      1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5 using System.Web;
    6
    7 namespace Plugins
    8 {
    9 public class CookieHelper
    10 {
    11
    12 #region 获取Cookie
    13
    14 /// <summary>
    15 /// 获得Cookie的值
    16 /// </summary>
    17 /// <param name="cookieName"></param>
    18 /// <returns></returns>
    19 public static string GetCookieValue(string cookieName)
    20 {
    21 return GetCookieValue(cookieName, null);
    22 }
    23
    24 /// <summary>
    25 /// 获得Cookie的值
    26 /// </summary>
    27 /// <param name="cookieName"></param>
    28 /// <param name="key"></param>
    29 /// <returns></returns>
    30 public static string GetCookieValue(string cookieName, string key)
    31 {
    32 HttpRequest request = HttpContext.Current.Request;
    33 if (request != null)
    34 return GetCookieValue(request.Cookies[cookieName], key);
    35 return "";
    36 }
    37
    38 /// <summary>
    39 /// 获得Cookie的子键值
    40 /// </summary>
    41 /// <param name="cookie"></param>
    42 /// <param name="key"></param>
    43 /// <returns></returns>
    44 public static string GetCookieValue(HttpCookie cookie, string key)
    45 {
    46 if (cookie != null)
    47 {
    48 if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
    49 return cookie.Values[key];
    50 else
    51 return cookie.Value;
    52 }
    53 return "";
    54 }
    55
    56 /// <summary>
    57 /// 获得Cookie
    58 /// </summary>
    59 /// <param name="cookieName"></param>
    60 /// <returns></returns>
    61 public static HttpCookie GetCookie(string cookieName)
    62 {
    63 HttpRequest request = HttpContext.Current.Request;
    64 if (request != null)
    65 return request.Cookies[cookieName];
    66 return null;
    67 }
    68
    69 #endregion
    70
    71 #region 删除Cookie
    72
    73 /// <summary>
    74 /// 删除Cookie
    75 /// </summary>
    76 /// <param name="cookieName"></param>
    77 public static void RemoveCookie(string cookieName)
    78 {
    79 RemoveCookie(cookieName, null);
    80 }
    81
    82 /// <summary>
    83 /// 删除Cookie的子键
    84 /// </summary>
    85 /// <param name="cookieName"></param>
    86 /// <param name="key"></param>
    87 public static void RemoveCookie(string cookieName, string key)
    88 {
    89 HttpResponse response = HttpContext.Current.Response;
    90 if (response != null)
    91 {
    92 HttpCookie cookie = response.Cookies[cookieName];
    93 if (cookie != null)
    94 {
    95 if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
    96 cookie.Values.Remove(key);
    97 else
    98 response.Cookies.Remove(cookieName);
    99 }
    100 }
    101 }
    102
    103 #endregion
    104
    105 #region 设置/修改Cookie
    106
    107 /// <summary>
    108 /// 设置Cookie子键的值
    109 /// </summary>
    110 /// <param name="cookieName"></param>
    111 /// <param name="key"></param>
    112 /// <param name="value"></param>
    113 public static void SetCookie(string cookieName, string key, string value)
    114 {
    115 SetCookie(cookieName, key, value, null);
    116 }
    117
    118 /// <summary>
    119 /// 设置Cookie值
    120 /// </summary>
    121 /// <param name="key"></param>
    122 /// <param name="value"></param>
    123 public static void SetCookie(string key, string value)
    124 {
    125 SetCookie(key, null, value, null);
    126 }
    127
    128 /// <summary>
    129 /// 设置Cookie值和过期时间
    130 /// </summary>
    131 /// <param name="key"></param>
    132 /// <param name="value"></param>
    133 /// <param name="expires"></param>
    134 public static void SetCookie(string key, string value, DateTime expires)
    135 {
    136 SetCookie(key, null, value, expires);
    137 }
    138
    139 /// <summary>
    140 /// 设置Cookie过期时间
    141 /// </summary>
    142 /// <param name="cookieName"></param>
    143 /// <param name="expires"></param>
    144 public static void SetCookie(string cookieName, DateTime expires)
    145 {
    146 SetCookie(cookieName, null, null, expires);
    147 }
    148
    149 /// <summary>
    150 /// 设置Cookie
    151 /// </summary>
    152 /// <param name="cookieName"></param>
    153 /// <param name="key"></param>
    154 /// <param name="value"></param>
    155 /// <param name="expires"></param>
    156 public static void SetCookie(string cookieName, string key, string value, DateTime? expires)
    157 {
    158 HttpResponse response = HttpContext.Current.Response;
    159 if (response != null)
    160 {
    161 HttpCookie cookie = response.Cookies[cookieName];
    162 if (cookie != null)
    163 {
    164 if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
    165 cookie.Values.Set(key, value);
    166 else
    167 if (!string.IsNullOrEmpty(value))
    168 cookie.Value = value;
    169 if (expires != null)
    170 cookie.Expires = expires.Value;
    171 response.SetCookie(cookie);
    172 }
    173 }
    174
    175 }
    176
    177 #endregion
    178
    179 #region 添加Cookie
    180
    181 /// <summary>
    182 /// 添加Cookie
    183 /// </summary>
    184 /// <param name="key"></param>
    185 /// <param name="value"></param>
    186 public static void AddCookie(string key, string value)
    187 {
    188 AddCookie(new HttpCookie(key, value));
    189 }
    190
    191 public static void CreateSecBrowse(string strHouseID)
    192 {
    193 string strRecord = CookieHelper.GetCookieValue("HistoreBrowse");
    194 string [] strItem=strRecord.Split(',');
    195 for (int i = 0; i < strItem.Length; i++)
    196 {
    197 if (strHouseID == strItem[i])
    198 {
    199 return;
    200 }
    201 }
    202 string strTempItem = strHouseID + ",";
    203 string strBrowseItem = string.Empty;
    204
    205 HttpCookie record = HttpContext.Current.Request.Cookies["HistoreBrowse"] ?? null;
    206 if (record == null)
    207 {
    208 record = new HttpCookie("HistoreBrowse");
    209 record.Value = strTempItem;
    210 strBrowseItem = record.Value;
    211 }
    212 else
    213 {
    214 record.Value = strTempItem + record.Value;
    215 strBrowseItem = record.Value;
    216
    217 }
    218 string[] strArray = strBrowseItem.Split(',');
    219
    220 StringBuilder sb = new StringBuilder();
    221
    222 if (strArray.Length > 4)
    223 {
    224 for (int i = 0; i < 4; i++)
    225 {
    226 sb.Append(strArray[i].ToString() + ",");
    227 }
    228 }
    229 else
    230 {
    231 for (int i = 0; i < strArray.Length; i++)
    232 {
    233 sb.Append(strArray[i].ToString()+",");
    234 }
    235 }
    236 string strTempCookies = sb.ToString().Substring(0,sb.ToString().LastIndexOf(","));
    237 record.Value = strTempCookies;
    238 record.Expires = DateTime.Now.AddDays(15);
    239 record.Path = "/";
    240 HttpContext.Current.Response.Cookies.Add(record);
    241 }
    242
    243 /// <summary>
    244 /// 添加Cookie
    245 /// </summary>
    246 /// <param name="key"></param>
    247 /// <param name="value"></param>
    248 /// <param name="expires"></param>
    249 public static void AddCookie(string key, string value, DateTime expires)
    250 {
    251 HttpCookie cookie = new HttpCookie(key, value);
    252 cookie.Expires = expires;
    253 AddCookie(cookie);
    254 }
    255
    256 /// <summary>
    257 /// 添加为Cookie.Values集合
    258 /// </summary>
    259 /// <param name="cookieName"></param>
    260 /// <param name="key"></param>
    261 /// <param name="value"></param>
    262 public static void AddCookie(string cookieName, string key, string value)
    263 {
    264 HttpCookie cookie = new HttpCookie(cookieName);
    265 cookie.Values.Add(key, value);
    266 AddCookie(cookie);
    267 }
    268
    269 /// <summary>
    270 /// 添加为Cookie集合
    271 /// </summary>
    272 /// <param name="cookieName">Cookie名称</param>
    273 /// <param name="expires">过期时间</param>
    274 public static void AddCookie(string cookieName, DateTime expires)
    275 {
    276 HttpCookie cookie = new HttpCookie(cookieName);
    277 cookie.Expires = expires;
    278 AddCookie(cookie);
    279 }
    280
    281 /// <summary>
    282 /// 添加为Cookie.Values集合
    283 /// </summary>
    284 /// <param name="cookieName"></param>
    285 /// <param name="key"></param>
    286 /// <param name="value"></param>
    287 /// <param name="expires"></param>
    288 public static void AddCookie(string cookieName, string key, string value, DateTime expires)
    289 {
    290 HttpCookie cookie = new HttpCookie(cookieName);
    291 cookie.Expires = expires;
    292 cookie.Values.Add(key, value);
    293 AddCookie(cookie);
    294 }
    295
    296 /// <summary>
    297 /// 添加Cookie
    298 /// </summary>
    299 /// <param name="cookie"></param>
    300 public static void AddCookie(HttpCookie cookie)
    301 {
    302 HttpResponse response = HttpContext.Current.Response;
    303 if (response != null)
    304 {
    305 //指定客户端脚本是否可以访问[默认为false]
    306 cookie.HttpOnly = true;
    307 //指定统一的Path,比便能通存通取
    308 cookie.Path = "/";
    309 //设置跨域,这样在其它二级域名下就都可以访问到了
    310 //cookie.Domain = "nas.com";
    311 response.AppendCookie(cookie);
    312 }
    313 }
    314
    315 #endregion
    316 }
    317 }
  • 相关阅读:
    linux(13)-如何创建软链接?
    linux(12)-如何操作允许root用户远程登录
    性能测试监控分析(14)-linux环境下性能监控命令dstat
    Codeforces Round #585 (Div. 2)E(状态压缩DP,思维)
    Codeforces Round #584
    【PAT甲级】1034 Head of a Gang (30 分)
    Educational Codeforces Round 72 (Rated for Div. 2)E(线段树,思维)
    【PAT甲级】1033 To Fill or Not to Fill (25 分)(贪心,思维可以做出简单解)
    【PAT甲级】1032 Sharing (25 分)
    【PAT甲级】1031 Hello World for U (20 分)
  • 原文地址:https://www.cnblogs.com/Archosaur/p/CookieHelper.html
Copyright © 2011-2022 走看看