lefodnes
asked on
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:
Then I'm trying to read this into a string in a .ashx file in the same directory:
Running that ashx-file gives me
I don't see the ÆØÅ
Checking the Request Headers in the debugging panel in Google Chrome for the ashx file, I see this:
No ÆØÅ there either...
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>
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;
}
}
}
Running that ashx-file gives me
TestingÆØÅ
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ÃÃÃ
No ÆØÅ there either...
ASKER
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?
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?
This page on experts-exchange should not interfereThat 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
ASKER
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: .. and this is the corresponding Chrome headers output
I also changed the ashx like this:
context.Response.ContentType = "text/plain; charset=UTF-8";
and the .html like this: <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
Unfortunately, it is the same result.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
Good, glad it's working.!
Open in new window