Juri Strumpflohner
Juri Strumpflohner Juri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops. Juri is also a recognized Google Developer Expert in Web Technologies

Problems with character encoding on cookies

1 min read

For preserving the search criteria on some page of our web application we're saving the different settings of the search filters into a cookie. So, when the user comes back next time we can restore the search filter settings that have been used previously.
Now however one of my working colleagues had the problem that some strings where not correctly retrieved from the cookie. Actually it was a character encoding problem. Characters like "ü,ö,.." were not displayed correctly after retrieving them from the cookie. I'm not sure whether you can set the character encoding of cookies. I don't think so, I've however not done any deep search on the web. Our solution was to do an url-encoding/decoding on the "critical" values of the cookie. So you can do something like
public void SaveValuesToCookie(){
...
Response.Cookies["key1"]["valuekey"] = Server.UrlEncode(criticalValue);
...
}

public void LoadValuesFromCookie(){
...
retrievedValue = Server.UrlDecode(Request.Cookies["key1"]["valuekey"]);
...
}

Questions? Thoughts? Hit me up on Twitter
comments powered by Disqus