• C#操作Cookie


    /* 创建者:菜刀居士的博客
     * 创建日期: 2014年09月02号
     * 功能:操作Cookie
     *
     */

    namespace Net.String.ConsoleApplication
    {
        using System;
        using System.Web;

        public static class CookieHelper
        {
            /// <summary>
            /// 加入cookie
            /// </summary>
            public static void AddCookie(this HttpContext h,string name, string value)
            {
                HttpCookie cookieName = new HttpCookie(name, System.Web.HttpUtility.UrlEncode(value, System.Text.Encoding.GetEncoding(65001)));
                h.Response.Cookies.Add(cookieName);
            }

            /// <summary>
            /// 加入cookie
            /// </summary>
            public static void AddCookie(this HttpContext h,string name, string value, TimeSpan span)
            {
                HttpCookie cookieName = new HttpCookie(name, System.Web.HttpUtility.UrlEncode(value, System.Text.Encoding.GetEncoding(65001)));

                cookieName.Expires = DateTime.Now.Add(span);

                h.Response.Cookies.Add(cookieName);
            }

            /// <summary>
            /// 得到cookie
            /// </summary>
            public static string GetCookie(this HttpContext h, string name)
            {
                if (h.Request.Cookies[name] != null)
                {
                    if (h.Response.Cookies.Count > 0 && h.Response.Cookies[name] != null)
                    {
                        return System.Web.HttpUtility.UrlDecode(h.Response.Cookies[name].Value, System.Text.Encoding.GetEncoding(65001));
                    }
                    return System.Web.HttpUtility.UrlDecode(h.Request.Cookies[name].Value, System.Text.Encoding.GetEncoding(65001));
                }
                else
                { return string.Empty; }
            }

            /// <summary>
            /// 删除cookie
            /// </summary>
            public static void RemoveCookie(this HttpContext h,string name)
            {
                h.Response.Cookies[name].Value = null;
                h.Response.Cookies[name].Expires = DateTime.Now.AddDays(-1);
            }

            /// <summary>
            /// 清空cookie
            /// </summary>
            public static void ClearCookie(this HttpContext h)
            {
                try
                {
                    foreach (HttpCookie hc in h.Response.Cookies)
                    {
                        hc.Value = null;
                        hc.Expires = DateTime.Now.AddDays(-1);
                    }
                }
                catch { }
            }
        }
    }

  • 相关阅读:
    默哀STAND SILENTLY!
    用虚拟机优化Windows(update:2008.4.24)
    UE的心情指数?
    God of War III 的发售日期?
    2009/8/15应该是一个愉快的夜晚.为林肯公园中国10月演唱会做好准备
    北京2008奥运会完美谢幕!
    《The Pursuit of Happyness / 当幸福来敲门》(2006)
    2007林肯公园上海演唱会观后感(实况像片/MP3) update:2008.1.31
    2008早上好
    Active Object C++智能指针实现
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5085554.html
Copyright © 2020-2023  润新知