HttpUtility.ParseQueryString() always encodes special characters to unicode

后端 未结 4 1677
隐瞒了意图╮
隐瞒了意图╮ 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: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()));
    

提交回复
热议问题