如何解码&;u0026&;在URL中
本文关键字:u0026 URL 何解码 解码 | 更新日期: 2023-09-27 18:04:47
我想解码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
!
您只需要这个函数
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(?<code>'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
。我建议围绕这一点编写一些单元测试,并使用各种输入作为开始。