• 设置,获取和删除Cookies


            #region 保存用户名的Cookie
            /// <summary>
            /// 设置Cookie
            /// </summary>
            /// <param name="cookieName"></param>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <param name="expires"></param>
            /// <returns></returns>
            public HttpCookie SetCookie(string cookieName, string key, string value, DateTime? expires)
            {
                HttpResponse response = System.Web.HttpContext.Current.Response;
                if (response != null)
                {
                    HttpCookie cookie = response.Cookies[cookieName];
                    if (cookie != null)
                    {
                        if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
                            cookie.Values.Set(key, value);
                        else
                            if (!string.IsNullOrEmpty(value))
                                cookie.Value = value;
                        if (expires != null)
                            cookie.Expires = expires.Value;
                        response.SetCookie(cookie);
                    }
                    return cookie;
                }
                else
                {
                    return null;
                }
            }
    
            /// <summary> 
            /// 获得Cookie 
            /// </summary> 
            /// <param name="cookieName"></param> 
            /// <returns></returns> 
            public HttpCookie GetCookie(string cookieName)
            {
                bool isReusable = HttpContext.CurrentHandler.IsReusable;
                HttpRequest request = System.Web.HttpContext.Current.Request;
                if (request != null)
                    return request.Cookies[cookieName];
                return null;
            }
            /// <summary>
            /// 删除Cookies
            /// </summary>
            /// <param name="cookieName"></param>
            /// <param name="key"></param>
            public static void RemoveCookie(string cookieName, string key)
            {
                HttpResponse response = System.Web.HttpContext.Current.Response;
                if (response != null)
                {
                    HttpCookie cookie = response.Cookies[cookieName];
                    if (cookie != null)
                    {
                        if (!string.IsNullOrEmpty(key))
                            cookie.Values.Remove(key);
                        else
                            response.Cookies.Remove(cookieName);
                    }
                }
    
            }
            #endregion


  • 相关阅读:
    MyBatis学习笔记
    通过Executor来启动线程比用Thread的start()更好
    步骤2:JMeter 分布式测试(性能测试大并发、远程启动解决方案)
    步骤1:JMeter 录制脚本接口测试
    adb常用命令(手机测试)
    电商购物核心功能测试点
    APP测试基本流程以及APP测试要点
    Jmeter-jtl性能测试报告转换-2种导出方法
    Linux之find查找命令
    linux之top命令详解
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3190221.html
Copyright © 2020-2023  润新知