首页 新闻 会员 周边

请问大家大型的网站cookies处理公共类如何构建(附有我的 )

0
[已关闭问题]

     /// <summary>
        /// 写cookie值单项赋值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="strValue">值</param>
        public static void SetCookie(string strName, string strValue)
        {
           
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
          
            if (cookie == null)
            {
                cookie = new HttpCookie(strName);
            }
            cookie.Value = HttpUtility.UrlEncode(strValue);
            HttpContext.Current.Response.AppendCookie(cookie);
        }

        /// <summary>
        /// cookeis集合赋值
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="strSend"></param>
        /// <param name="strvalue"></param>
        public static void SetCookie(string strName, string strSend, string strvalue)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
            {
                cookie = new HttpCookie(strName);
            }

            cookie.Values[strSend] =HttpUtility.UrlEncode(strvalue);
            HttpContext.Current.Response.AppendCookie(cookie);
        }


        public static bool blCookies(string StrName)
        {
            if (HttpContext.Current.Request.Cookies != null)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[StrName];

                if (cookie == null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 提取cookies集合的值
        /// </summary>
        /// <param name="strName">集合</param>
        /// <param name="strSend">子项</param>
        /// <returns></returns>
        public static string GetCookie(string strName, string strSend)
        {
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName].Values != null)
            {
                if (HttpContext.Current.Request.Cookies[strName].Values[strSend] != null)
                {
                    return HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[strName].Values[strSend].ToString());
                }
                else
                {
                    return null;
                }

            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到cookeis时间
        /// </summary>
        /// <returns></returns>
        public DateTime GetExpriresTime()
        {
            string CookiesName = GetType().ToString();
            if (HttpContext.Current.Request.Cookies[CookiesName] != null)
            {
                return HttpContext.Current.Request.Cookies[CookiesName].Expires;
            }
            return DateTime.MinValue;
        }

 

        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="strValue">值</param>
        /// <param name="strValue">过期时间(分钟)</param>
        /// <param name="DoMain">cookie的域</param>
        public static void SetCookie(string strName, string strValue, int expires, string DoMain)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
            {
                cookie = new HttpCookie(strName);
            }
            cookie.Value = HttpUtility.UrlEncode(strValue);
            cookie.Expires = DateTime.Now.AddMinutes(expires);
            HttpContext.Current.Response.AppendCookie(cookie);
        }

        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="StrName">名称</param>
        /// <param name="StrValue">值</param>
        /// <param name="Expires">过期时间</param>
        /// <param name="DoMain">相关联的域</param>
        public static void SetCookies(string StrName, string StrValue, int Expires, string DoMain)
        {
            HttpCookie cookies = HttpContext.Current.Request.Cookies[StrName];
            if (cookies== null)
            {
                cookies = new HttpCookie(StrName);
            }
            cookies.Value = HttpUtility.UrlEncode(StrValue);
            cookies.Expires = DateTime.Now.AddMinutes(Expires);
            HttpContext.Current.Response.AppendCookie(cookies);
        }

        /// <summary>
        /// 读cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <returns>cookie值</returns>
        public static string GetCookie(string strName)
        {
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
            {
                Encoding stre = Encoding.GetEncoding("UTF-8");
                string str = HttpContext.Current.Request.Cookies[strName].Value.ToString();
                return HttpUtility.UrlDecode(str, stre);
            }
            else
            {
                return "";
            }
        }

 

 

亮颖一生的主页 亮颖一生 | 初学一级 | 园豆:147
提问于:2010-03-23 11:42
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册