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;
                }
            }
    
        }
    

      

  • 相关阅读:
    redis
    linux systemd
    分布式高可用架构演进
    c++ 多线程 信号量简单使用
    JAVA上传文件到FTP上
    JAVA字符串去掉html代码
    jQuery判断复选框是否勾选
    SpringBoot全局异常处理
    SpringBoot统一日志打印
    JAVA加解密之DES
  • 原文地址:https://www.cnblogs.com/Kuleft/p/11088198.html
Copyright © 2011-2022 走看看