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
C# .net中cookie值为中文时的乱码解决方法的更多相关文章
- Eclipse导入的工程后referenced libraries中的jar包中文注释显示乱码解决方法
Preferences-General-Workspace-Text file encoding 设置为uft-8 最后重启一下eclipse.
- asp.net url址址中中文汉字参数传递乱码解决方法
中文乱码是网站开发中会常碰到的问题,今天我们来讲一下关于url址址中中文汉字参数传递乱码解决方法,有需要的朋友可以参考下.在cs文件里传参的时候用UrlEncode: Response.Redirec ...
- JabRef中添加中文文献出现乱码 解决方法
JabRef中添加中文文献出现乱码 解决方法 问题描述 JaBRef是一款开源的文献管理软件,主要用来管理bibtex格式的参考文献,可以与LATEX配合使用,方便论文参考文献的使用.文献管理 ...
- python listdir() 中文路径 中文文件夹 乱码 解决方法
python listdir() 中文路径 中文文件夹 乱码 解决方法 listdir(path)返回的结果的编码似乎和我们提供的 path 参数的编码有关: path = 'd:/test' try ...
- Struts2中action接收中文参数为乱码解决方法
老实说,中文乱码问题是每个程序员会经常遇到的问题,而且也是一个很头疼的问题.网上很多关于解决中文乱码的帖子,看几个之后你会发现大都是一样的.但是我们照着做,却还是无法解决乱码问题.我也是看了好多帖子, ...
- 读取properties配置文件,value值为中文时出现乱码
已确保idea工具默认设置都是UTF-8格式: 然后在配置类上,指定编码: @PropertySource(value = "classpath:short_message.prop ...
- thinkphp 中 dump 函数调试数组时显示不全解决方法
在php.ini里的 xdebug 节点中,加入如下 xdebug.var_display_max_children=128 #子级节点最多显示的的字符数xdebug.var_display_max_ ...
- VS Code 中文注释显示乱码 解决方法
将设置中的"files.autoGuessEncoding"项的值改为true即可. 1.文件 2.首选项 3.设置 4.搜索 "files.autoGuessEncod ...
- 吴裕雄--天生自然HTML学习笔记:启动TOMCAT服务器时出现乱码解决方法
随机推荐
- 从Eclipse迁移到Android Studio
Google正式推出了Android Studio 1.0,Android默认的开发工具也由Eclipse变成了intellij,对Eclipse的支持肯定会越来越少了,对于Android开发者来说, ...
- Android数据存储五种方式总结
本文介绍Android平台进行数据存储的五大方式,分别如下: 1 使用SharedPreferences存储数据 2 文件存储数据 3 SQLite数据库存储数据 4 使用Cont ...
- Log4jConfigListener cannot be found — context fails to start
解决方案 Reticulogic's second suggestion is correct. However, in Eclipse Helios, the "Java EE Modul ...
- [转载]Emmet使用
转载地址:http://www.iteye.com/news/27580 Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语 ...
- hibernate注解——@Temporal
@Temporal(TemporalType.TIMESTAMP) @Column(name="createTime",nullable=false) private Date c ...
- [20140722] forwarded和forwarding记录
背景: 今天被人文集forwarded和forwarding记录的事情. 简单介绍: 当堆表跟新某一个列的时候发现,不够放了,那么就在那行记录上标记forwarding,并把数据放到另外一个page, ...
- Consul 服务发现和配置
Service discovery and configuration made easy. Distributed, highly available, and datacenter-aware. ...
- ubuntu同时安装qt4.8和qt5.7
这是ubuntu默认安装(从apt安装)的路径和相关文件,建议编译安装到/opt目录下,使用./configure --prefix=/opt/Qt4.8 /usr/share/qt4 /usr/sh ...
- Java Hello World例子和添加按钮事件与功能
新建android工程,然后默认“下一步”即可完成创建: 2.添加Button 3.在src的MainActivity.java添加以下红色代码 import android.support.v7.a ...
- 用Android Studio 开发Android应用
目前AndroidStudio已经到了1.2版本了,我用了下,觉得还蛮好的,有些自动生成的资源,它会自动帮你管理.下面开始列一下,我的开发环境搭配.在开始前,你得有个VPN,可者代理.嗯.不然你下不了 ...