java中Cookie中文字符乱码问题
生活随笔
收集整理的這篇文章主要介紹了
java中Cookie中文字符乱码问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
如果Cookie中的Value 中有中文字符出現,在加入Cookie的時候,會出現下面的錯誤:
java.lang.IllegalArgumentException: Control character in cookie value or attribute.
當我們設定Cookie的Value的值得時候:
cookie.setValue(ret); 改為如下方式盡心編碼!
cookie.setValue(URLEncoder.encode(ret, "utf-8"));使用指定的編碼機制將字符串轉換為 application/x-www-form-urlencoded 格式,中文字符是兩個字節。
當我們在取出Cookie的Value的值時,同樣利用相應的解碼:
String val = cookie.getValue();
val = URLDecoder.decode(val, "utf-8");?使用指定的編碼機制對 application/x-www-form-urlencoded 字符串解碼。
java.lang.IllegalArgumentException: Control character in cookie value or attribute.
當我們設定Cookie的Value的值得時候:
cookie.setValue(ret); 改為如下方式盡心編碼!
cookie.setValue(URLEncoder.encode(ret, "utf-8"));使用指定的編碼機制將字符串轉換為 application/x-www-form-urlencoded 格式,中文字符是兩個字節。
當我們在取出Cookie的Value的值時,同樣利用相應的解碼:
String val = cookie.getValue();
val = URLDecoder.decode(val, "utf-8");?使用指定的編碼機制對 application/x-www-form-urlencoded 字符串解碼。
?
轉載于:https://www.cnblogs.com/hujunzheng/p/4112256.html
總結
以上是生活随笔為你收集整理的java中Cookie中文字符乱码问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (九)linux中断编程
- 下一篇: 长17米高60公分,24的三角形墙需要多