• asp.net操作cookie类,包含datatable批量存入cookie


    以下是类:

       public class CookieMgr {
            #region 快速储存Cookie
            /// <summary>
            /// 快速储存Cookie
            /// </summary>
            /// <param name="dt">数据源表</param>
            /// <param name="cookieName">Cookie名称</param>
            /// <param name="day">过期时间,天</param>
            public static void SaveCookie(DataTable dt, string cookieName, int day) {
                HttpCookie cookie = new HttpCookie(cookieName);
                if (day != 0)
                    cookie.Expires = DateTime.Now.AddDays(day);
    
                for (int i = 0; i < dt.Columns.Count; i++) {
                    cookie.Values.Add(dt.Columns[i].ColumnName, StringMgr.Encrypt(dt.Rows[0][dt.Columns[i].ColumnName].ToString())); //加密
                }
                HttpContext.Current.Response.AppendCookie(cookie);
            }
            #endregion
    
            #region 初始化一个Cookie
            /// <summary>
            /// 初始化一个Cookie
            /// </summary>
            /// <param name="cookieName">cookie名称</param>
            /// <param name="day">过期时间,天</param>
            public static void InitCookie(string cookieName, int day) {
                HttpCookie cookie = new HttpCookie(cookieName);
                if (day != 0)
                    cookie.Expires = DateTime.Now.AddDays(day);
    
                cookie.Values.Add("initExpires", StringMgr.Encrypt(day.ToString()));
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
            #endregion
    
            #region 设置Cookie的其中一个值,如果键存在则覆盖以前的键值
            /// <summary>
            /// 附加一个Cookie的值,如果键值存在则覆盖以前的键值
            /// </summary>
            /// <param name="cookieName">Cookie名称</param>
            /// <param name="key">需附加的键</param>
            /// <param name="value">需附加的值</param>
            public static void SetCookie(string cookieName, string key, string value) {
                HttpCookie cookie = null;
                if (HttpContext.Current.Request.Cookies[cookieName] != null) {
                    cookie = HttpContext.Current.Request.Cookies[cookieName];
                } else
                    cookie = new HttpCookie(cookieName);
    
                bool b = false;
                for (int i = 0; i < cookie.Values.Keys.Count; i++) {
                    if (cookie.Values.Keys[i] == key) {
                        b = true;
                        break;
                    }
                }
                value = StringMgr.Encrypt(value);
                if (b)
                    cookie.Values[key] = value;
                else
                    cookie.Values.Add(key, value);
    
    
                for (int i = 0; i < cookie.Values.Keys.Count; i++) {
                    if (cookie.Values.Keys[i] == "initExpires") {
                        cookie.Expires = DateTime.Now.AddDays(int.Parse(StringMgr.Decrypt(cookie.Values["initExpires"].ToString())));
                        break;
                    }
                }
    
    
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
            #endregion
    
            #region 读取Cookie的其中一个值
            /// <summary>
            /// 读取Cookie的其中一个值
            /// </summary>
            /// <param name="cookieName">Cookie名称</param>
            /// <param name="key">需读取的键</param>
            /// <returns>返回名为【CookieName】键为【key】的值</returns>
            public static string GetCookie(string cookieName, string key) {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
                if (cookie != null && cookie.Values[key] != null)
                    return StringMgr.Decrypt(cookie.Values[key].ToString());
                else
                    return Param.CookieErrorString;
            }
            #endregion
    
            #region 移除Cookie的其中一个值
            /// <summary>
            /// 移除Cookie的其中一个值
            /// </summary>
            /// <param name="cookieName">Cookie名称</param>
            /// <param name="key">需附加的键</param>
            public static void RemoveCookie(string cookieName, string key) {
                HttpCookie cookie = null;
                if (HttpContext.Current.Request.Cookies[cookieName] != null)
                    cookie = HttpContext.Current.Request.Cookies[cookieName];
                else
                    cookie = new HttpCookie(cookieName);
    
                bool b = false;
                for (int i = 0; i < cookie.Values.Keys.Count; i++) {
                    if (cookie.Values.Keys[i] == key) {
                        b = true;
                        break;
                    }
                }
                if (b)
                    cookie.Values.Remove(key);
    
                for (int i = 0; i < cookie.Values.Keys.Count; i++) {
                    if (cookie.Values.Keys[i] == "initExpires") {
                        cookie.Expires = DateTime.Now.AddDays(int.Parse(StringMgr.Decrypt(cookie.Values["initExpires"].ToString())));
                        break;
                    }
                }
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
            #endregion
    
            #region 清空Cookie
            /// <summary>
            /// 清空Cookie
            /// </summary>
            /// <param name="cookieName">Cookie名称</param>
            public static void ClearCookie(string cookieName) {
                System.Web.HttpCookie cookie = new System.Web.HttpCookie(cookieName);
                cookie.Expires = System.DateTime.Now.AddYears(-1);
                System.Web.HttpContext.Current.Response.AppendCookie(cookie);
            }
            #endregion
    
            #region 得到Cookie的集合
            /// <summary>
            /// 得到Cookie的集合
            /// </summary>
            /// <param name="cookieName">Cookie名称</param>
            /// <returns>返回Cookie的集合</returns>
            public static NameValueCollection GetCookieValues(string cookieName) {
                if (HttpContext.Current.Request.Cookies[cookieName] != null) {
                    HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
                    return cookie.Values;
                } else
                    return null;
            }
            #endregion
        }

    保存DT到cookie

    ookieMgr.SaveCookie(dt, "名称", 0);

    获取cookie key值

    CookieMgr.GetCookie("名称", "姓名");

  • 相关阅读:
    文件上传---普通文件fileupload.jar和url文件httpUrlConnection
    HttpClient学习整理
    编写更少量的代码:使用apache commons工具类库
    多线程进阶
    多线程下HashMap的死循环问题
    线程本地变量ThreadLocal源码解读
    Eclipse工作常见问题总结
    Java集合---ConcurrentHashMap原理分析
    Java集合---Arrays类源码解析
    Java集合---LinkedList源码解析
  • 原文地址:https://www.cnblogs.com/hfzsjz/p/3195207.html
Copyright © 2020-2023  润新知