在Asp.net的HttpCOOKIE中写入汉字,读取值为什么全是乱码?其实这是因为文字编码而造成的,汉字是两个编码,所以才会搞出这么个乱码出来!
其实解决的方法很简单:只要在写入COOKIE时,先将其用Url编码,然后再写入,当我们读取时再解码就OK
例子:
COOKIE的写入:
HttpCOOKIE COOKIE=new HttpCOOKIE("Simple");
COOKIE.Values.Add("Simple1",HttpUtility.UrlEncode("大叔,你好!"));
COOKIE.Values.Add("Simple2","English is OK!Nothing we should do!");
Response.AppendCOOKIE(COOKIE);
COOKIE的读取:
HttpCOOKIE COOKIE=Request.COOKIEs["Simple"];
string simple1=HttpUtility.UrlDecode(COOKIE["Simple1"]);
string simple2=COOKIE["Simple2"];