{"id":669,"date":"2005-04-01T22:55:00","date_gmt":"2005-04-02T02:55:00","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=669"},"modified":"2005-04-01T22:55:00","modified_gmt":"2005-04-02T02:55:00","slug":"asp-java-cookies-and-urlencode","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2005\/04\/01\/asp-java-cookies-and-urlencode\/","title":{"rendered":"ASP, Java, Cookies and URLEncode"},"content":{"rendered":"<p>One of the bugs I tracked down a couple months ago dealt with the different ways in which ASP and Java handle URL encoding in a cookies.  ASP automatically encodes and decodes cookie values (without you telling it too even though it the Server object has a method &#8216;URLEncode&#8217; which does the same job) while Java requires that you explicity use the URLDecoder and URLEncoder, otherwise it sends the cookie values without encoding them first, which in my case lead to an interoperability problem (one assumes URL encoding of cookie values, one doesn&#8217;t).  <\/p>\n<p>But then it gets weirder.  The encoding that ASP automatically applies is different than the encoding that Java applies.  For example, in ASP you&#8217;d write something like this to set a cookie with my email address:<br \/>\n<code><br \/>\nResponse.cookies(\"encoded\") = \"ajohnson@cephas.net\"<br \/>\n<\/code><br \/>\nwhich would then result in this:<br \/>\n<code><br \/>\najohnson%40cephas%2Enet<br \/>\n<\/code><br \/>\nwhere in Java you&#8217;d have something like this:<br \/>\n<code><br \/>\nString email = \"ajohnson@cephas.net\";<br \/>\nString encoded = URLEncoder.encode(email, \"UTF-8\");<br \/>\nres.addCookie(new Cookie(\"encoded\", encoded););<br \/>\n<\/code><br \/>\nwhich results in this:<br \/>\n<code><br \/>\najohnson%40cephas.net<br \/>\n<\/code><br \/>\nSo why does ASP encode the period as %2E and Java leave it alone? The Microsoft documentation for the ASP Server object contains very little documentation about the <a href=\"http:\/\/www.microsoft.com\/windows2000\/en\/server\/iis\/htm\/asp\/vbob0wkl.htm\">URLEncode<\/a> method other than to say that the URLEncode method <\/p>\n<blockquote><p>&#8230; applies URL encoding rules, including escape characters, to a specified string.\n<\/p><\/blockquote>\n<p>(which could be a teensy bit more helpful, must have been a programmer writing that documentation).  On the other hand, the Java <a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/net\/URLEncoder.html\">URLEncoder class documenation<\/a> says that the special characters &#8220;.&#8221;, &#8220;-&#8220;, &#8220;*&#8221;, and &#8220;_&#8221; are not to be encoded according to the <a href=\"http:\/\/www.w3.org\/TR\/html4\/\">HTML specification<\/a>, which itself cites <a href=\"http:\/\/www.ietf.org\/rfc\/rfc1738.txt\">RFC 1738<\/a> which says:<\/p>\n<blockquote><p>\n&#8230;the special characters &#8220;$-_.+!*'(),&#8221;, and reserved characters used for their reserved purposes may be used unencoded within a URL.\n<\/p><\/blockquote>\n<p>which seems to me to imply that the ASP Cookie object is wrong (ie: it should leave the period alone in the email address).  And beyond that, it&#8217;s pretty aggressive in assuming that I want to encode the data in my cookies. Am I missing something?  How do other languages (PHP, Python, Perl, etc..) implement the same functionality?  Aren&#8217;t web applications fun?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the bugs I tracked down a couple months ago dealt with the different ways in which ASP and Java handle URL encoding in a cookies. ASP automatically encodes and decodes cookie values (without you telling it too even though it the Server object has a method &#8216;URLEncode&#8217; which does the same job) while &hellip; <a href=\"https:\/\/cephas.net\/blog\/2005\/04\/01\/asp-java-cookies-and-urlencode\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ASP, Java, Cookies and URLEncode<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,3,2],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/669"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=669"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/669\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}