Why do cookie values with whitespace arrive at the client side with quotes?

前端 未结 4 1134
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 09:17

I\'m a .NET developer starting to dabble in Java.

In .NET, I can set the value of a cookie to a string with white space in it: new HttpCookie(\"myCookieName\"

4条回答
  •  悲哀的现实
    2020-11-28 09:48

    As far as I know, spaces must be encoded in cookies. Different browsers react differently to un-encoded cookies. You should URL-encode your cookie before setting it.

    String cookieval = "my value";
    String cookieenc = URLEncoder.encode(cookieval, "UTF-8");
    res.addCookie(new Cookie("myCookieName", cookieenc));
    

    ASP.NET does the encoding automatically, in Java you have to do it yourself. I suspect the quotes you see are added by the user agent.

提交回复
热议问题