Cookie功能封装类

    public class Cookies
    {
        /// <summary>
        /// 添加Cookies
        /// </summary>
        /// <param name="strName">主键</param>
        /// <param name="strValue">值</param>
        /// <param name="strDay">保存时间</param>
        public static void Add(string strName, string strValue, DateTime strDay)
        {
            try
            {
                HttpCookie Cookie = new HttpCookie(strName);
                Cookie.Expires = strDay;
                Cookie.Value = HttpUtility.UrlEncode(strValue);
                System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
            }
            catch
            { }
        }
        /// <summary>
        /// 读取一个指定的Cookies
        /// </summary>
        /// <param name="strName">主键</param>
        /// <returns></returns>
        public static string Value(string strName)
        {
            HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
            if (Cookie != null)
            {
                return HttpUtility.UrlDecode(Cookie.Value.ToString());
            }
            return null;
        }
        /// <summary>
        /// 删除Cookies
        /// </summary>
        /// <param name="strName">主键</param>
        public static void Remove(string strName)
        {
            try
            {
                HttpCookie Cookie = new HttpCookie(strName);
                Cookie.Expires = DateTime.Now.AddDays(-1);
                System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
            }
            catch
            { }
        }
        /// <summary>
        /// 判断指定的Cookies是否存在
        /// </summary>
        /// <param name="strName">主键</param>
        /// <returns></returns>
        public static bool Exis(string strName)
        {
            try
            {
                return !Value(strName).Equals(null);
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 获取指定的Cookies和指定的内容作对比
        /// </summary>
        /// <param name="strName">主键</param>
        /// <param name="strValue">对比内容</param>
        /// <returns></returns>
        public static bool CompareTo(string strName, string strValue)
        {
            try
            {
                return Value(strName).Equals(strValue);
            }
            catch
            {
                return false;
            }
        }
    }

posted @ 2010-06-24 12:06  davidDJT  阅读(194)  评论(0)    收藏  举报