Link to home
Create AccountLog in
Avatar of webressurs
webressursFlag for Norway

asked on

Problem with ÆØÅ characters when using http headers (Web API)

In my ASP.Net Web Api I have a Get function where input info is sent in the http header. It works fine, except when the http header contains Norwegian characters like Æ, Ø and Å. This characters is transformed to æ ø Ã¥. When the same characters (Æ,Ø,Å) are sent as parameters in the URL it works fine. When testing the script on my local PC it works fine to send Æ, Ø and Å in the http header as well.

I don't know why norwegian characters can't be sent in the http header, but I guess this is an encoding / charset problem. After hours of testing I still can't find any way fo fix it.

Web.config:

<globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" culture="nb-NO" uiCulture="nb-NO" />

Open in new window


Getting Http Header:
 
private string GetHeader(string key)
{
    var header = "";
    if (Request.Headers.Contains(key))
    {
        IEnumerable<string> values = new List<string>();
        Request.Headers.TryGetValues(key, out values);
        if (!string.IsNullOrEmpty(values.First())) header = values.First();
    }
    return header;
}
 
var headerUserName = GetHeader("UserName");

Open in new window


Request headers

User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
UserName: ÆØÅ
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
 

Response headers

Date: Wed, 15 May 2013 12:46:19 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 4.0.30319
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Content-Type: application/json; charset=utf-8
Content-Length: 4174
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Is it browser specific problem?
The request is using ISO-8859-1 before utf-8  the response is just utf-8. Try removing ISO-8859-1
Avatar of webressurs

ASKER

The only solution that works for me is to UrlEncode the HTTP Header value, and UrlDecode it on the server.
ASKER CERTIFIED SOLUTION
Avatar of Rob
Rob
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
That was the solution :)