When using HttpUtility from System.Web, I find that everytime I call the method .ParseQueryString I am having special characters encode to their unicode equivalent representations. I have tried with many different encoding types, and all seem to produce the same result. An example of my code is here:
string text = "ich möchte diese Bild für andere freigeben"
var urlBuilder = new UriBuilder(url);
var query = HttpUtility.ParseQueryString(urlBuilder.Query, Encoding.UTF8);
query["text"] = text;
urlBuilder.Query = query.ToString();
string finalUrl = urlBuilder.ToString();
And the string in finalUrl
that I would recieve from this would be:
text=ich+m%u00f6chte+diese+Bild+f%u00fcr+andere+freigeben
I have tried using Encoding.UTF8
,Encoding.ASCII
and Encoding.Default
and they all produce the same result. What can I do to reach my desired format of UrlEncoding:
text=ich%20m%C3%B6chte%20diese%20Bild%20f%C3%BCr%20andere%20freigeben
As always, Thanks in advance for the help/advice!
The problem is in:
urlBuilder.Query = query.ToString();
HttpUtility.ParseQueryString
returns a NameValueCollection
but is actually an internal class called HttpValueCollection
. This class has an override of the ToString()
method. It generates an encoded query string but for its URL encoding it uses HttpUtility.UrlEncodeUnicode
(tinyurl.com/HttpValue). This results in the %uXXXX values.
If you need a different type of URL encoding you might want to avoid HttpUtility.ParseQueryString
or decode the result of ToString()
and encode it afterwards:
urlBuilder.Query = Uri.EscapeUriString(HttpUtility.UrlDecode(query.ToString()));
This question is rather old, but I just came across it while researching this problem and noticed that it's missing a valid answer.
The fix is fairly simple, in the web.config
simply add the following setting (tested and works in .NET 4.5):
<appSettings>
<add key="aspnet:DontUsePercentUUrlEncoding" value="true" />
</appSettings>
Setting this value to true controls how .NET will encode certain characters in the URL. Specifically characters like ä, ë, ö etc. I think this might be because there are several ways these characters can be encoded. The way this generally done is with the prefix %C3
which denotes that the following character has an umlaut (I'm fairly sure that's how it works).
The way HttpUtility.ParseQueryString
does it by default is different. It encodes the character to the actual percentage encoded unicode character %u00f6
. This can cause some issues because it's not the default even within .NET itself, HttpUtility.UrlEncode
for instance will encode it to %C3%B6
. Changing the above setting will make sure that both methods return similar results.
I am not familiar with ParseQueryString, but it appears from the documentation to convert a properly formatted query into name value pairs. From your post it appears you are trying to do the opposite: convert data pairs to a properly formatted query. Instead you may try using HttpUtility.UrlEncode
string text = "ich möchte diese Bild für andere freigeben"
var urlBuilder = new UriBuilder(url);
String query = "text=" + HttpUtility.UrlEncode(text);
urlBuilder.Query = query;
string finalUrl = urlBuilder.ToString();
Use: System.Web.HttpUtility.ParseQueryString(Request.Url.Query, UTF8Encoding.Default)
For Example: www.mydomain.com/page?name=Jia+Almi%F1a&PAYID=123456&TOWN=LONDON
Actual Name: Jia Almiña
Request.Querystring["name"]: Jia Almi�a (which isn't correct)
First get the Raw url which will be
Request.Url.Query: ?name=Jia+Almi%F1a&PAID=123456&TOWN=LONDON
System.Web.HttpUtility.ParseQueryString(Request.Url.Query, UTF8Encoding.Default).Get("name") will be Jia Almiña