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

      

  • 相关阅读:
    关于INTEL FPGA设计工具DSP Builder
    关于FPGA电源精度要求
    基于INTEL FPGA硬浮点DSP实现卷积运算
    Redis 笔记(四)—— SET 常用命令
    Redis 笔记(三)—— LIST 常用命令
    Redis 笔记(二)—— STRING 常用命令
    Redis 练习(二)
    Redis 练习(一)
    Redis 笔记(一)——数据类型简介
    Java 中为什么不能创建泛型数组?
  • 原文地址:https://www.cnblogs.com/Kuleft/p/11088198.html
Copyright © 2020-2023  润新知