How to decode “\u0026” in a URL?

佐手、 提交于 2019-12-21 11:35:11

问题


I want decode URL A to B:

A) http:\/\/example.com\/xyz?params=id%2Cexpire\u0026abc=123

B) http://example.com/xyz?params=id,expire&abc=123

This is a sample URL and I look for a general solution not A.Replace("\/", "/")...

Currently I use HttpUtility.UrlDecode(A, Encoding.UTF8) and other Encodings but cannot generate URL B !


回答1:


You only need this function

System.Text.RegularExpressions.Regex.Unescape(str);



回答2:


This is a basic example I was able to come up with:

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();
}

This is probably missing a lot depending on the types of URLs you are going to get. It doesn't handle error checking, escaping of literals, like \\u0026 should be \u0026. I'd recommend writing a few unit tests around this with various inputs to get started.



来源:https://stackoverflow.com/questions/6990347/how-to-decode-u0026-in-a-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!