zoukankan      html  css  js  c++  java
  • asp.net读取asp的cookies 及乱码问题

    System.Text.Encoding   enc   =   System.Text.Encoding.GetEncoding("gb2312");     
      
    string   cookie   =   Request.Cookies["YourCookieName"].Value;     
      
    string   cookieValue   =   HttpUtility.UrlDecode(   cookie,enc);
     System.Text.Encoding enc = System.Text.Encoding.GetEncoding("gb2312");
            
    for (int i = 0; i < HttpContext.Current.Request.Cookies.Count; i++)
            {
                
    if (HttpContext.Current.Request.Cookies.Keys[i] == null)
                    
    return;
                
                    
    if (HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies.Keys[i]).ToString() == "admin_session_offices")
                      admin_session_offices 
    = HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[i].Value.ToString(), enc);

                  
    if (HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies.Keys[i]).ToString() == "admin_session_caozuos")
                      admin_session_caozuos 
    = HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[i].Value.ToString(), enc);

                  
    if (HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies.Keys[i]).ToString() == "admin_session_cishu")
                      admin_session_cishu 
    = HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[i].Value.ToString(), enc);                    
            
                
    ////输出测试
                //Response.Write(HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies.Keys[i], enc) + ":" + HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[i].Value.ToString(), enc) + "<br>");
            } 

    //************************** 关于键名乱码的解决方案

    <div style="padding-top: 4px; text-align: right; font-weight: normal">
               
    <%=request.Cookies("admin_session_offices")%>
               
    &nbsp;&nbsp;<%=request.Cookies("admin_session_caozuos")%>&nbsp;&nbsp;<br>
                累计登陆
    <%=request.Cookies("admin_session_cishu")%>&nbsp;&nbsp;</div>

    ---------------------------------------------以上是asp的读取。。
    aspx:

    HttpContext.Current.Request.Cookies["admin_session_caozuos"].Value 报错,而
    HttpContext.Current.Request.Cookies["admin%5Fsession%5Fcaozuos"].Value 这样的取值却有效果,
    如何统一把cookie里的键及值重新解码??

  • 相关阅读:
    关于 拼接 url 连接 参数的问题(爬虫)。
    date 时间确定
    JavaScript跨域解决方法大全
    JS的 try catch使用心得
    博客要搬家啦!
    bzoj 3676: [Apio2014]回文串 -- 回文自动机
    bzoj 2631: tree -- LCT
    bzoj 2002: [Hnoi2010]Bounce 弹飞绵羊 -- LCT
    bzoj 2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 --kdtree
    bzoj 1978: [BeiJing2010]取数游戏 game -- dp
  • 原文地址:https://www.cnblogs.com/Fooo/p/1442349.html
Copyright © 2011-2022 走看看