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

      

  • 相关阅读:
    AIX系统/var/adm/wtmp大文件处理
    script & scriptreplay
    Ubuntu/Debianpxe/isopreseed
    Ubuntu12.04安装gimp-2.8
    Ubuntu 3D特效一览
    Unix history图览
    Undelete Files on Linux Systems
    开源界有趣的循环缩写和LOGO
    Ubuntu上的dock
    linux下歌曲、视频、文件等乱码
  • 原文地址:https://www.cnblogs.com/Kuleft/p/11088198.html
Copyright © 2020-2023  润新知