cookies读写代码

public class DoCookie
    {
        /// <summary>
        /// 写入cookie值 
        /// </summary>
        /// <param name="strName">cookie名称</param>
        /// <param name="strValue">cookie值</param>
        /// <param name="strDay">有效期(单位:天)</param>
        /// <returns>是否设置成功</returns>
        public static bool setCookie(string strName, string strValue, int strDay)
        {
            try
            {
                HttpCookie Cookie = new HttpCookie(strName);
                Cookie.Expires = DateTime.Now.AddDays(strDay);
                Cookie.Value = strValue;
                System.Web.HttpContext.Current.Response.AppendCookie(Cookie);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 写入cookie键值
        /// </summary>
        /// <param name="strName">cookie名</param>
        /// <param name="key">cookie键</param>
        /// <param name="strValue">cookie键值</param>
        /// <returns></returns>
        public static bool setCookie(string strName, string key, string strValue)
        {
            try
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
                if (cookie == null)
                {
                    cookie = new HttpCookie(strName);
                }
                cookie[key] = strValue;
                HttpContext.Current.Response.AppendCookie(cookie);
                return true;
            }
            catch
            {
                return false;
            }
        }


        /// <summary>
        /// 写入cookie键值并设置其域
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="key"></param>
        /// <param name="strValue"></param>
        /// <param name="cookieDomain"></param>
        /// <returns></returns>
        public static bool setCookie(string strName, string key, string strValue, string cookieDomain)
        {
            try
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
                if (cookie == null)
                {
                    cookie = new HttpCookie(strName);
                }
                cookie.Domain = cookieDomain;
                cookie[key] = strValue;
                HttpContext.Current.Response.AppendCookie(cookie);
                return true;
            }
            catch
            {
                return false;
            }
        }



        /// <summary>
        /// 获取cookie值
        /// </summary>
        /// <param name="strName">cookie名称</param>
        /// <returns>cookie值</returns>
        public static string getCookie(string strName)
        {
            HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
            if (Cookie != null)
            {
                return Cookie.Value.ToString();
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获取cookie
        /// </summary>
        /// <param name="strName">cookie名</param>
        /// <param name="key">cookie键值</param>
        /// <returns></returns>
        public static string getCookie(string strName, string key)
        {
            HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
            if (Cookie != null)
            {
                return Cookie[key];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 删除cookie
        /// </summary>
        /// <param name="strName">cookie名称</param>
        /// <returns>是否删除成功</returns>
        public static bool delCookie(string strName)
        {
            try
            {
                HttpCookie Cookie = new HttpCookie(strName);
                Cookie.Values.Clear();
                Cookie.Expires = DateTime.Now.AddYears(-1);
                System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 删除相关域的cookie
        /// </summary>
        /// <param name="strName">cookie名称</param>
        /// <returns>是否删除成功</returns>
        public static bool delCookie(string strName, string cookieDomain)
        {
            try
            {
                HttpCookie Cookie = new HttpCookie(strName);
                Cookie.Values.Clear();
                Cookie.Expires = DateTime.Now.AddYears(-1);
                Cookie.Domain = cookieDomain;
                System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
                return true;
            }
            catch
            {
                return false;
            }
        }

    }

  

posted @ 2019-06-26 09:56  kuleft  阅读(496)  评论(0编辑  收藏  举报