Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with UTF-8 character encoding from URL query string value in Internet Explorer 9

I'm finding an odd issue in Internet Explorer, specifically IE9, when trying to display special characters (German accented characters) provided within the URL query string. This is working as expected in Firefox and Chrome.

For example, the URL I'm working with looks something like this:

http://mysite.com/TestPage.aspx?Title=Hochauflösendes®

I've also tried the URL encoded version of the URL:

http://mysite.com/TestPage.aspx?Title=Hochaufl%C3%B6sendes%C2%AE

In either case, when I'm trying to display that 'Title' query string value on my page using Request.QueryString["Title"], IE doesn't display the characters correctly:

Hochaufl�sendes�

If I hard-code the text directly on the page, it displays properly on all browsers. It's only when it's pulling from the query string where the issue occurs.

The page is saved as UTF-8 encoding, and I have the meta tag in my page as necessary:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

I've also looked at the page's header and content via Fiddler, and all the encoding is correct.

What could be causing IE not to display the special characters properly?

like image 739
cjsharp1 Avatar asked Jan 31 '13 22:01

cjsharp1


People also ask

Are urls UTF-8 or ASCII?

The UTF-8 locale support addresses this need. Browsers are limited to a defined character set that can legally be used in a uniform resource locator (URL). This range is defined to be the printable characters in the ASCII character set (between hex code 0x20 and 0x7e).

What is a UTF-8 encoded string?

UTF-8 encodes a character into a binary string of one, two, three, or four bytes. UTF-16 encodes a Unicode character into a string of either two or four bytes. This distinction is evident from their names. In UTF-8, the smallest binary representation of a character is one byte, or eight bits.

Should I always use UTF-8?

When you need to write a program (performing string manipulations) that needs to be very very fast and that you're sure that you won't need exotic characters, may be UTF-8 is not the best idea. In every other situations, UTF-8 should be a standard. UTF-8 works well on almost every recent software, even on Windows.

Why is UTF-8 a good choice?

Why use UTF-8? An HTML page can only be in one encoding. You cannot encode different parts of a document in different encodings. A Unicode-based encoding such as UTF-8 can support many languages and can accommodate pages and forms in any mixture of those languages.


1 Answers

As suggested by Aristos, using HttpContext.Current.Request.RawUrl worked for my situation.

To retrieve the actual query string value from the RawUrl, a simple method like this can be used:

private string GetQueryStringValueFromRawUrl(string queryStringKey)
{
    var currentUri = new Uri(HttpContext.Current.Request.Url.Scheme + "://" + 
        HttpContext.Current.Request.Url.Authority + 
        HttpContext.Current.Request.RawUrl);
    var queryStringCollection = HttpUtility.ParseQueryString((currentUri).Query);
    return queryStringCollection.Get(queryStringKey);
}

Retrieving the value using this method was tested as working in IE8 and IE9. The bug is fixed in IE10.

like image 180
cjsharp1 Avatar answered Oct 05 '22 21:10

cjsharp1