Cookie取值问题。
我做一了注册页,注册成功后生成一个多值Cookie,然后进入发布信息页,想把多值Cookie取出来,可是姓名乱码,电话号和姓名放在一起了,而且电话的Cookie值把电话的Cookie名也显示出来,其它几项正确,高手指点!
------解决方案--------------------问题:存到Cookie时用URLEncoder.encode( "中文 ", "UTF-8 "),但取出来时按 "UTF-8 "进行转码却不行(ISO-8859-1也不行),什么原因呢?
写Cookie:
Cookie chineseCookie = new Cookie( "chineseCookie ", URLEncoder.encode( "中文 ", "UTF-8 "));
chineseCookie.setMaxAge(36000);
response.setCharacterEncoding( "UTF-8 ");
response.addCookie(chineseCookie);
读Cookie:
Cookie[] cookies = request.getCookies();
for (int i = 0; i < cookies.length; i++) {
if ( "chineseCookie ".equals(cookies[i].getName())) {
String temp = cookies[i].getValue();
temp = URLDecoder.decode(temp, "UTF-8 ");
System.out.println( "chineseCookie : " + temp); //测试打印语句
}
------解决方案--------------------// write
HttpCookies cookie = new HttpCookies( "MyCookieName ");
cookie.Value = Server.UrlEncode( "王二 ");
Response.Cookies.Add(cookie);
// read
HttpCookies cookie = Request.Cookies[ "MyCookieName "];
string myName = Server.UrlDecode(cookie.Value);