`

Cookies and Unicode characters

阅读更多

转载:http://blog.madskristensen.dk/post/Cookies-and-Unicode-characters.aspx

Cookies and Unicode characters

by Mads Kristensen 9. September 2007

I’ve been having some issues with storing Unicode characters in cookies today. Whenever a cookie is set and the value filled with Unicode characters, the same characters cannot be retrieved from the cookie again. When they are retrieved from the requesting browser, they are changed into something totally unreadable.

Background

The cookie is set when a visitor enters some text into a textbox and submits the form. When the same visitor returns to that page I wanted to pre-fill the textbox with the value submitted earlier. Very easy and simple and not before someone noticed the strange behaviour with Unicode characters I thought it worked just fine.

Because the value was displayed in a textbox I thought that maybe HTML encoding could solve the issue. Don’t ever HTML encode a cookie in ASP.NET! It results in a yellow screen of death and an exception stating that the cookie contains dangerous characters. The dangerous character it was referring to was a HTML encoded representation of a Unicode character and looked something like this "#248;". The only thing to do is to delete your cookies in order to view that page again.

The solution

It took me a while to figure it out, but all you need to do is to URL encode the cookie value. It works no matter what encoding you use for the page. The example below illustrates the very simple solution:

private void SetCookie()
{
  HttpCookie cookie = new HttpCookie("cookiename");
  cookie.Expires = DateTime.Now.AddMonths(24);
  cookie.Values.Add("name", Server.UrlEncode(txtName.Text));
  Response.Cookies.Add(cookie);
}

private void GetCookie()
{
  HttpCookie cookie = Request.Cookies["cookiename"];
  if (cookie != null)
  {
    txtName.Text = Server.UrlDecode(cookie.Values["name"]);
  }
}

It is so simple but caused me a lot of time investigating and clearing cookies from the browser.

分享到:
评论

相关推荐

    UE(官方下载)

    In this tutorial, we'll cover some of the basics of Unicode-encoded text and Unicode files, and how to view and manipulate it in UltraEdit. Search and delete lines found UEStudio and UltraEdit provide...

    Python Cookbook, 2nd Edition

    Converting Between Characters and Numeric Codes Recipe 1.3. Testing Whether an Object Is String-like Recipe 1.4. Aligning Strings Recipe 1.5. Trimming Space from the Ends of a String Recipe ...

    python3.6.5参考手册 chm

    PEP 471 - os.scandir() function – a better and faster directory iterator PEP 475: Retry system calls failing with EINTR PEP 479: Change StopIteration handling inside generators PEP 485: A function...

    part3 JS Additional articles.pdf

    - **Escaping, Special Characters**:转义字符和特殊字符的使用。 - **Sets and Ranges**:定义字符集和范围 `[a-z]`。 - **Quantifiers**:如 `+`, `*`, `?`, `{n}` 控制字符出现的次数。 - **Greedy and Lazy ...

    Python Power - The Comprehensive Guide (2008).pdf

    And Now for Something Completely Different… ....................................................18 CHAPTER 2 Python Language Overview ....................................................................

Global site tag (gtag.js) - Google Analytics