zoukankan      html  css  js  c++  java
  • C# .net中cookie值为中文时的乱码解决方法

    C# .net中cookie值为中文时的乱码解决方法

    一.cookie的名称或子cookie的名称不能为中文,否则无法获得cookie

    这个好办,名称不用中文即可

    二.cookie的值为中文时候,取cookie的值会出现乱码

    解决办法:存取cookie时候先解码和编码

    存cookie,进行编码:

    cookie.Value = HttpUtility.UrlEncode("上海");

    取cookie时候,进行解码:
    cookieValue = HttpUtility.UrlDecode(cookie.Value);

    另外注意:

    取子cookie时候,应该先解码,再根据%号分解,才能取到子cookie的值.

    写Cookie里,一定要加上UrlEncode,用Request读Cookie时,一定要加上UrlDecode

    示例如下:
    protected void Page_Load(object sender, EventArgs e)
         {
             HttpCookie GoodList = null;

             //如果GoodList不为空,则GOODID+1,GoodsName加"乐无烟"
             if (Request.Cookies["GoodList"] != null)
             {
                 GoodList = Request.Cookies["GoodList"];
                 GoodList.Values["GoodsID"] = Server.UrlEncode(Server.UrlDecode(Request.Cookies["GoodList"]["GoodsID"]) + ",1");
                 GoodList.Values["GoodsName"] = Server.UrlEncode(Server.UrlDecode(Request.Cookies["GoodList"]["GoodsName"]) + ",乐无烟");
                 GoodList.Expires = DateTime.Now.AddDays(1);
                 Response.Cookies.Add(GoodList);
             }
             else
             {
                 //初次写入Cookie
                 GoodList = new HttpCookie("GoodList");
                 GoodList.Values["GoodsID"] = Server.UrlEncode("2");
                 GoodList.Values["GoodsName"] = Server.UrlEncode("无烟锅");
                 GoodList.Expires = DateTime.Now.AddDays(1);
                 Response.Cookies.Add(GoodList);
             }

             Response.Write(Server.UrlDecode(Request.Cookies["GoodList"]["GoodsID"]) + "<br/>");
             Response.Write(Server.UrlDecode(Request.Cookies["GoodList"]["GoodsName"]) + "<hr/>");
    }

    中文cookie的问题,
    在Windows 2000正常,
    在Windows 2003 sp1下会偶尔出现乱码(遇到双字节特殊字符时候,例子:「`蹆绌 ),
    在windows 2003 SP2下基本乱码

    解决办法:
    采用
    Server.UrlEncode();
    Server.UrlDecode();
    Cookie["MyCookie"] = Server.UrlEncode("中文")
    Response.Write(Server.UrlDecode(Request.Cookies("MyCookie").Value()))
    编码和解码。


    另外编码和解码要一致
    System.Web.HttpUtility.UrlDecode 和 System.Web.HttpUtility.UrlEncode
    System.Web.HttpContext.Current.Server.UrlDecode 和 System.Web.HttpContext.Current.Server.UrlEncode

    JS、C#编码解码

    原文: https://www.cnblogs.com/lmfeng/archive/2011/11/08/2240991.html

    escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z

    encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z

    encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z

    1.

    JS: escape :

    • js使用数据时可以使用escape
    • 例如:搜藏中history纪录。
    • 0-255以外的unicode值进行编码时输出%u****格式,其它情况下escape,encodeURI,encodeURIComponent编码结果相同。

    解码使用:unescape

    C#:

    HttpUtility.UrlEncode   
    HttpUtility.UrlDecode

    2.

    JS: encodeURI :

    • 进行url跳转时可以整体使用encodeURI
    • 例如:Location.href=encodeURI("http://cang.baidu.com/do/s?word=百度&ct=21");

    解码使用decodeURI();

    C#: decodeURIComponent

    3.

    JS: encodeURIComponent :

    • 传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。                           
    • 例如:<script language="javascript">document.write('<a href="http://passport.baidu.com/?logout&aid=7& u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a& gt;');</script>

    解码使用decodeURIComponent()

    C#:

    [HttpContext.Current.]Server.UrlDecode

    [HttpContext.Current.]Server.UrlEncode

  • 相关阅读:
    【WPF】操作RichTextBox(取值、赋值、清空、滚动条自动滚动实例、文本自动滚动实例)
    系统初始化 服务列表
    多个filter如何决定调用顺序
    IE浏览器 查看Form对象
    java try_catch 分析
    关于ClassLoader 和Class的俩个记录
    lis分析之一一批处理(任务)如何连接数据库的
    document.all("div).style.display = "none"与 等于""的区别
    Mybatis Util包
    Spring创建bean对象的三种方式
  • 原文地址:https://www.cnblogs.com/xcsn/p/5048085.html
Copyright © 2011-2022 走看看