如何解码URL中的“\ u0026”?
我想解码URL A
到B
:
A) http:\/\/example.com\/xyz?params=id%2Cexpire\u0026abc=123
B) http://example.com/xyz?params=id,expire&abc=123
这是一个示例URL,我寻找一般的解决方案,而不是A.Replace("\/", "/")...
目前我使用HttpUtility.UrlDecode(A, Encoding.UTF8)
和其他Encodings
但无法生成URL B
!
你只需要这个function
System.Text.RegularExpressions.Regex.Unescape(str);
这是我能够提出的基本示例:
static void Sample() { var str = @"http:\/\/example.com\/xyz?params=id%2Cexpire\u0026abc=123"; str = str.Replace("\\/", "/"); str = HttpUtility.UrlDecode(str); str = Regex.Replace(str, @"\\u(?
\d{4})", CharMatch); Console.Out.WriteLine("value = {0}", str); } private static string CharMatch(Match match) { var code = match.Groups["code"].Value; int value = Convert.ToInt32(code, 16); return ((char) value).ToString(); }
这可能会遗漏很多,具体取决于您将获得的URL类型。 它不处理错误检查,文件的转义,如\\u0026
应该是\u0026
。 我建议围绕此编写一些unit testing,并开始使用各种输入。