Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decode "\u0026" in a URL?

Tags:

c#

url

urldecode

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 !

like image 210
Xaqron Avatar asked Aug 09 '11 01:08

Xaqron


People also ask

How do you decode a space in a URL?

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.

What is URL decode encode?

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.

How do I decode a Google URL?

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.


2 Answers

You only need this function

System.Text.RegularExpressions.Regex.Unescape(str);
like image 65
Bikrone Avatar answered Sep 21 '22 06:09

Bikrone


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.

like image 27
vcsjones Avatar answered Sep 19 '22 06:09

vcsjones