Javascript encoding cookie to be read by asp.net in different encoding

I'm having difficulties understanding the encoding "circus"... Again.
I'm writing a cookie in javascript in a plain HTML file, like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script>
        document.cookie = "testCookie=TestingÆØÅ";
    </script>
</head>
<body>

</body>
</html>

Open in new window


Then I'm trying to read this into a string in a .ashx file in the same directory:
<%@ WebHandler Language="C#" Class="readcookie" %>

using System;
using System.Web;

public class readcookie : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        HttpCookie hc = context.Request.Cookies["testCookie"];
        if (hc != null)
        {
            string s = hc.Value;
            context.Response.Write(s);
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

Open in new window


Running that ashx-file gives me
TestingÆØÅ

Open in new window


I don't see the ÆØÅ

Checking the Request Headers in the debugging panel in Google Chrome for the ashx file, I see this:
GET /readcookie.ashx HTTP/1.1
Host: 127.0.0.1
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: nb-NO,nb;q=0.8,no;q=0.6,nn;q=0.4,en-US;q=0.2,en;q=0.2
Cookie: testCookie=TestingÆØÅ

Open in new window


No ÆØÅ there either...
lefodnesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
I don't know what the solution is but you have not declared the character encoding in any of those files.  UTF8 is the most common recommendation these days but it doesn't necessarily display correctly in your editor... if your editor is not using UTF8.  Firefox says the character encoding on this page is Unicode which usually means UTF8.  Line 4 on this page is:
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

Open in new window

0
lefodnesAuthor Commented:
This page on experts-exchange should not interfere, since it is displaying the characters as I see them in my browser and editors (visually).
I tried adding <meta charset="UTF-8" /> in the .html file that sets the cookie, but there was the same result. All files are saved as utf-8 encoding. I think IIS looks at this when serving the files to the browser. I'm suspecting that the javascript and the browser is running UCS-2 or UTF-16, and maybe I should convert in asp.net, but I would like to have an expert's opinion on this. What would be the recommended way of doing this?
0
Dave BaldwinFixer of ProblemsCommented:
This page on experts-exchange should not interfere
That is a misconception that I have seen here quite a number of times.  The character set encoding of this page determines the character set used by ALL text on this page.  People have complained that their posts don't look like what they thought they posted and that is always the reason why.

One of the biggest reasons that character encoding is a problem is that changing the declaration does not change the character codes.  The character set declaration is there to tell the browser what character set to use in displaying the text... using the character codes that are in the page text.

Here is the Microsoft page on the subject:  https://msdn.microsoft.com/en-us/library/39d1w2xf%28v=vs.140%29.aspx
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

lefodnesAuthor Commented:
That is a misconception...
Well. I'm just saying it doesn't interfere with the .html and .ashx files I am running on my local IIS.

I'm guessing we're both seeing the same thing on this EE page, unless one of us have a totally messed up browser. It is declaring UTF-8, and both our browsers are using that encoding.

As I wrote, on my machine, it looks the same on the EE Firefox output and the local file Chrome output.
This is my ashx file: Screen shot of ashx output.. and this is the corresponding Chrome headers output
Screen shot of headers sectionI also changed the ashx like this:
context.Response.ContentType = "text/plain; charset=UTF-8";

Open in new window

and the .html like this:
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />

Open in new window

Unfortunately, it is the same result.
0
Dave BaldwinFixer of ProblemsCommented:
I'm seeing the same thing that you show in your screen shots.  If you click on the Cookies tab, is the display the same?  

Looks like IIS and/or ASP.NET are altering it somehow.  I assume that ASCII characters don't get changed?

It appears that your characters in your cookie value should be encoded with encodeURI() since they are not all ASCII characters.  http://www.w3schools.com/tags/ref_urlencode.asp
Original Cookie spec: http://curl.haxx.se/rfc/cookie_spec.html
Current RFC : http://tools.ietf.org/html/rfc6265
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lefodnesAuthor Commented:
Took a while before I got to test this, but now I did. It solved the problem, and now all letters are correctly read by the server side.
0
Dave BaldwinFixer of ProblemsCommented:
Good, glad it's working.!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.