zoukankan      html  css  js  c++  java
  • 解决cookie 不能存中文的方案

    cookie 不支持中文 

    解决方案非常容易    只需将其UrlEncode 一下即可,读取的时候要UrlDecode   方便起见,来个CookieHelper 类来操作

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
     
    namespace Sheb.Common
    {
        public class CookieHelper 
        {
            /// <summary>
            /// 增加一个cookie 记录
            /// </summary>
            /// <param name="cookie"></param>
            public static void AddCookie(HttpCookie cookie)
            {
                DateTime dtNow = System.DateTime.Now;
                DateTime CookieExpries = cookie.Expires;
                TimeSpan span=  CookieExpries - dtNow;
     
                cookie.Value = HttpUtility.UrlEncode(cookie.Value);
     
                if (span.TotalDays < 0)
                {
                    cookie.Expires = dtNow.AddDays(1);
                }
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
            /// <summary>
            /// 判断是否有cookie 
            /// </summary>
            /// <param name="cookieName"></param>
            /// <returns></returns>
            public static bool HasCookie(string cookieName)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
                if (cookie == null ||
                    (cookie.Expires < System.DateTime.Now) && cookie.Expires != System.DateTime.MinValue)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            /// <summary>
            /// 删除一个cookie 
            /// </summary>
            /// <param name="cookieName"></param>
            public static void DelCookie(string cookieName)
            {
                if (HttpContext.Current.Request.Cookies[cookieName] != null)
                {
                    HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
                    cookie.Expires = System.DateTime.Now.AddDays(1);
                    HttpContext.Current.Response.Cookies.Add(cookie);
                }
            }
     
            /// <summary>
            /// 获得cookie 的值
            /// </summary>
            /// <param name="cookieName"></param>
            /// <returns></returns>
            public static string GetCookieValue(string cookieName)
            {
                if (HasCookie(cookieName))
                {
                    
                    return HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[cookieName].Value);
                }
                else
                {
                    return "";
                }
            }
        }
    }
  • 相关阅读:
    python调试代码好的方法
    Java保留两位小数的几种写法总结
    SPRING BOOT 项目中使用<SCOPE>PROVIDED</SCOPE>打包成WAR到TOMCAT运行出现的问题总结
    Spring Boot整合Thrift RPC
    Thrift语法参考
    Thrift中enum的一些探究
    Thrift入门及Java实例演示
    xcrun: error: unable to find utility "xctest", not a developer tool or in PATH
    Composer: Command Not Found
    Mac安装thrift因bison报错的解决办法
  • 原文地址:https://www.cnblogs.com/jicheng1014/p/1562396.html
Copyright © 2011-2022 走看看