代码之家  ›  专栏  ›  技术社区  ›  Joe Phillips

HttpUtility.UrlEncode带大写十六进制字符

  •  0
  • Joe Phillips  · 技术社区  · 7 年前

    HttpUtility.UrlEncode("abc : 123") 生产 abc+%3a+123 但我需要它来生产 abc+%3A+123 (注意大写字母 A .

    有没有办法让UrlEncode输出大写十六进制字符?

    我不想打电话给你 .ToUpper() abc 保持小写。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Mel Gerats    7 年前

    这不支持开箱即用,但您可以自己完成:

    private static string UrlEncodeUpperCase(string stringToEncode)
    {
        var reg = new Regex(@"%[a-f0-9]{2}");
        stringToEncode = HttpUtility.UrlEncode(stringToEncode);
        return reg.Replace(stringToEncode, m => m.Value.ToUpperInvariant());
    }
    

    请记住 [RFC 3986][1] 明确提到大写和小写是等价的。

        2
  •  0
  •   Joe Phillips    7 年前

    我相信我已经从这个问题的第二个答案中找到了答案: https://stackoverflow.com/a/1148326/20471

    解决方法是使用 Uri.EscapeDataString %20 用于空格而不是 + 还有大写的十六进制替换。