如果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
字符串解碼。
?