0

我有一个格式为RFC 3986的编码字符串%x##。例如,空格字符被编码为%x20而不是%20. 如何在 C# 中对其进行解码?使用 decode 方法或Uriclasses未解码字符串。HttpUtilityWebUtility

4

2 回答 2

1

您可以尝试正则表达式以匹配Replace所有%x##内容和%##匹配项:

  using System.Text.RegularExpressions;

  ...

  string demo = "abc%x20def%20pqr";

  string result = Regex.Replace(
      demo, 
    "%x?([0-9A-F]{2})", 
      m => ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString(), 
      RegexOptions.IgnoreCase);

  Console.Write(result);

结果:

  abc def pqr
于 2020-01-09T10:36:33.970 回答
0

你可以尝试这样的事情: 你可以尝试:

参考:如何让 Uri.EscapeDataString 符合 RFC 3986

    var escapedString = new StringBuilder(Uri.EscapeDataString(value));

    for (int i = 0; i < UriRfc3986CharsToEscape.Length; i++) {
        escapedString.Replace(UriRfc3986CharsToEscape[i], Uri.HexEscape(UriRfc3986CharsToEscape[i][0]));
    }

    // Return the fully-RFC3986-escaped string.
    return escaped.ToString();
于 2020-01-09T11:08:20.500 回答