Solved

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

Posted on 2013-05-20
5
772 Views
Last Modified: 2013-06-18
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
0
Comment
Question by:webressurs
  • 2
  • 2
5 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 39182964
Is it browser specific problem?
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39183095
The request is using ISO-8859-1 before utf-8  the response is just utf-8. Try removing ISO-8859-1
0
 
LVL 1

Author Comment

by:webressurs
ID: 39185050
The only solution that works for me is to UrlEncode the HTTP Header value, and UrlDecode it on the server.
0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 39186307
That sounds like a solution to me.  is that not suitable?
0
 
LVL 1

Author Closing Comment

by:webressurs
ID: 39256332
That was the solution :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now