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
!
Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format. URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.
Simply put, URL encoding translates special characters from the URL to a representation that adheres to the spec and can be correctly understood and interpreted. In this tutorial, we'll focus on how to encode/decode the URL or form data so that it adheres to the spec and transmits over the network correctly.
You can decode them by either using a keyboard shortcut (CTRL + SHIFT + ALT + D), or by clicking on the decode URLS context menu link, or by clicking on decode URLS from the plugin icon at the top.
You only need this function
System.Text.RegularExpressions.Regex.Unescape(str);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With