HttpUtility.ParseQueryString() always encodes special characters to unicode

后端 未结 4 1665
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 14:16

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 represent

相关标签:
4条回答
  • 2020-12-16 14:56

    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();
    
    0 讨论(0)
  • 2020-12-16 14:59

    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()));
    
    0 讨论(0)
  • 2020-12-16 15:00

    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

    0 讨论(0)
  • 2020-12-16 15:01

    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.

    0 讨论(0)
提交回复
热议问题