• Cookie值中文乱码问题


    cookie里面不能写中文,是由于cookie先天的编码方式造成的。所以需要有一种中间编码来过渡。 URLEncode是最好的选择。

    我们以asp.net为例,代码如下:

    设置Cookie时:

    HttpCookie cookie = new HttpCookie("name", System.Web.HttpContext.Current.Server.UrlEncode("雨林星空"));
    Response.Cookies.Add(cookie);

    读取Cookie时:

    if (Request.Cookies["name"] != null)
    {
        Response.Write(System.Web.HttpContext.Current.Server.UrlDecode(Request.Cookies["name"].Value));
    }
    
    

    注意:编码和解码要一致

    System.Web.HttpContext.Current.Server.UrlDecode 和 System.Web.HttpContext.Current.Server.UrlEncode

    System.Web.HttpUtility.UrlDecode 和 System.Web.HttpUtility.UrlEncode

  • 相关阅读:
    活动安排
    中国剩余定理
    欧拉回路
    单词游戏
    Ant Trip
    John's Trip
    太鼓达人
    相框
    原始生物
    Blockade
  • 原文地址:https://www.cnblogs.com/codeloves/p/2914057.html
Copyright © 2020-2023  润新知