zoukankan      html  css  js  c++  java
  • Cookies操作类

    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;

    /// <summary>
    ///Cookies 的摘要说明
    /// </summary>
    public class Cookies
    {
     public Cookies()
     {
      //
      //TODO: 在此处添加构造函数逻辑
      //
     }
        /// <summary>
        /// 判断cookie是否存在
        /// </summary>
        /// <param name="strName">cookie名称</param>
        public static bool IsExists(String strName)
        {
            return (System.Web.HttpContext.Current.Request.Cookies[strName] == null) ? false : true;
        }
        /// <summary>
        /// 判断cookie是否存在
        /// </summary>
        /// <param name="strName">cookie名称</param>
        /// <param name="strKey">键</param>
        public static bool IsExists(String strName, String strKey)
        {
            if (System.Web.HttpContext.Current.Request.Cookies[strName] == null)
                return false;
            else
                return (String.IsNullOrEmpty(System.Web.HttpContext.Current.Request.Cookies[strName][strKey])) ? false : true;
        }
        /// <summary>
        /// 写入cookie
        /// </summary>
        /// <param name="strName">cookie名称</param>
        /// <param name="strValue">值</param>
        public static void WriteCookie(String strName, String strValue)
        {
            System.Web.HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
                cookie = new System.Web.HttpCookie(strName);

            cookie.Value = HttpUtility.UrlEncode(strValue);

            System.Web.HttpContext.Current.Response.AppendCookie(cookie);
        }
        /// <summary>
        /// 写入cookie
        /// </summary>
        /// <param name="strName">cookie名称</param>
        /// <param name="strKey">键</param>
        /// <param name="strValue">值</param>
        public static void WriteCookie(String strName, String strKey, String strValue)
        {
            System.Web.HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
                cookie = new System.Web.HttpCookie(strName);

            cookie[strKey] = strValue;
            System.Web.HttpContext.Current.Response.AppendCookie(cookie);
        }
        /// <summary>
        /// 写入cookie
        /// </summary>
        /// <param name="strName">cookie名称</param>
        /// <param name="strKey">键</param>
        /// <param name="strValue">值</param>
        /// <param name="expires">cookie到期时间(分钟)</param>
        public static void WriteCookie(String strName, String strKey, String strValue, Int32 expires)
        {
            System.Web.HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
                cookie = new System.Web.HttpCookie(strName);

            cookie[strKey] = strValue;
            cookie.Expires = DateTime.Now.AddMinutes(expires);
            System.Web.HttpContext.Current.Response.AppendCookie(cookie);
        }
        /// <summary>
        /// 写入cookie
        /// </summary>
        /// <param name="strName">cookie名称</param>
        /// <param name="strValue">值</param>
        /// <param name="expires">cookie到期时间(分钟)</param>
        public static void WriteCookie(String strName, String strValue, Int32 expires)
        {
            System.Web.HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
                cookie = new System.Web.HttpCookie(strName);

            cookie.Value = strValue;
            cookie.Expires = DateTime.Now.AddMinutes(expires);
            System.Web.HttpContext.Current.Response.AppendCookie(cookie);
        }
        /// <summary>
        /// 获取cookie值
        /// </summary>
        /// <param name="strName">cookie名称</param>
        public static String GetCookie(String strName)
        {
            //return (System.Web.HttpContext.Current.Request.Cookies == null || System.Web.HttpContext.Current.Request.Cookies[strName] == null) ?
            //    String.Empty : System.Web.HttpContext.Current.Request.Cookies[strName].Value;

            return (System.Web.HttpContext.Current.Request.Cookies == null || System.Web.HttpContext.Current.Request.Cookies[strName] == null) ?
              String.Empty : HttpUtility.UrlDecode(System.Web.HttpContext.Current.Request.Cookies[strName].Value);
        }
        /// <summary>
        /// 获取cookie值
        /// </summary>
        /// <param name="strName">cookie名称</param>
        /// <param name="strKey">键</param>
        public static String GetCookie(String strName, String strKey)
        {
            return (System.Web.HttpContext.Current.Request.Cookies == null ||
                System.Web.HttpContext.Current.Request.Cookies[strName] == null ||
                String.IsNullOrEmpty(System.Web.HttpContext.Current.Request.Cookies[strName][strKey])) ?
                String.Empty : System.Web.HttpContext.Current.Request.Cookies[strName][strKey];
        }
        /// <summary>
        /// 清空cookie
        /// </summary>
        /// <param name="strName">cookie名称</param>
        public static void ClearCookie(String strName)
        {
            System.Web.HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
                cookie = new System.Web.HttpCookie(strName);

            cookie.Expires = DateTime.Now.AddSeconds(-1);
            System.Web.HttpContext.Current.Response.AppendCookie(cookie);
        }


    }

  • 相关阅读:
    0923------APUE 学习笔记----------Linux系统的启动流程
    0915-----Linux设备驱动 学习笔记----------一个简单的字符设备驱动程序
    0815------算法笔记----------矩阵连乘问题
    0806------Linux网络编程----------Echo 网络库 学习笔记
    事件学习
    信息系统需求分析阶段的实践经验之二---如何有效地获得用户需求【转】
    信息系统需求分析阶段的实践经验之一---需求分析概述[转]
    Lambda表达式【转】
    C#委托的介绍(delegate、Action、Func、predicate)【转】
    css3选择器——导图篇
  • 原文地址:https://www.cnblogs.com/kiwifruit/p/1863154.html
Copyright © 2011-2022 走看看