zoukankan      html  css  js  c++  java
  • 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;
                }
            }
    
        }
    

      

  • 相关阅读:
    用几何画板画三星状图形的方法有哪些
    ChemDraw 15.1 Pro插入阿尔法可以这样做
    用MathType编辑异或与非符号有什么方法
    整合Thinkphp数据库基本操作CURD,界面datagrid采用EasyUi的Demo
    可编辑表格
    jQuery一步一步实现跨浏览器的可编辑表格,支持IE、Firefox、Safari、
    jfinal 使用类里的方法
    左右值无限分类实现算法
    PHP递归实现无限级分类
    ThinkPHP自动填充实现无限级分类的方法
  • 原文地址:https://www.cnblogs.com/Kuleft/p/11088198.html
Copyright © 2011-2022 走看看