I have a JavaScript request going to a ASP.Net (2.0) HTTP handler which passes the request to a java web service. In this system special characters, such as those with an accent do not get passed on correctly.
E.G.
- Human input:
Düsseldorf
- becomes a JavaScript asynch request to
http://site/serviceproxy.ashx?q=D%FCsseldorf
, which is valid in ISO-8859-1 as well as in UTF-8 as far as I can tell. (unless it's %c3%bc in UTF-8) HttpContext.Current.Request.QueryString.Get("q")
returnsD�sseldorf
which is where trouble begins.- but
HttpUtility.UrlEncode(HttpContext.Current.Request.QueryString.Get("q"), Encoding.GetEncoding("ISO-8859-1"))
returnsD%3fsseldorf
(a '?') - and
HttpUtility.UrlEncode(HttpContext.Current.Request.QueryString.Get("q"), Encoding.UTF8)
returnsD%ef%bfsseldorf
So it the value doesn't get decoded nor re-encoded correctly to be passed on to the java service.
- Notice
HttpContext.Current.Request.Url.Query
is?q=D%FCsseldorf&output=json&from=1&to=10
- while
HttpContext.Current.Request.QueryString.ToString()
isq=D%ufffdsseldorf&output=json&from=1&to=10
Why is this, and how can I tell the HttpContext
to honor the request headers which include:
Content-Type=application/x-www-form-urlencoded;+charset=UTF-8
and decode the URL's QueryString
using the UTF-8 charset.
Addendum: As the answer notes, the trouble lies not so much in the decoding as the encoding; using escape()
in JavaScript does not escape according to UTF-8, while using encodeURIComponent()
does.