pad0n
pad0n

Reputation: 317

URL unicode parameters decoding C#

I got a URL which contains parameters, one of which is with Cyrillic letters.

http://localhost/Print.aspx?id=4&subwebid=243572&docnumber=%u0417%u041f005637-1&deliverypoint=4630013519990

Doc-number must be ЗП005637-1. I have tried the following code, but string is still with those characters %u0417%u041f.

public static String DecodeUrlString(this String url)
    {
        String newUrl;
        while ((newUrl = Uri.UnescapeDataString(url)) != url)
            url = newUrl;
        return newUrl;
    }

It's not a possibility to use HttpUtility.

Upvotes: 3

Views: 875

Answers (1)

sstan
sstan

Reputation: 36483

If your goal is to avoid a dependency on System.Web.dll, then you would normally use the equivalent method in the WebUtility Class: WebUtility.UrlDecode Method.

However, you will find that, even then, your url won't get decoded the way you want it to.

This is because WebUtility.UrlDecode does not handle the %uNNNN escape notation on purpose. Notice this comment in the source code:

// *** Source: alm/tfs_core/Framework/Common/UriUtility/HttpUtility.cs
// This specific code was copied from above ASP.NET codebase.
// Changes done - Removed the logic to handle %Uxxxx as it is not standards compliant.

As stated in the comment, the %uNNNN escape format is not standard compliant and should be avoided if possible. You can find more info on this and on the proper way of encoding urls from this thread.

If you have any control over how the url is generated, consider changing it to be standard-compliant. Otherwise, consider adding System.Web.dll as a dependency, find another third-party library that does the job, or write your own decoder. As commented already, the source code is out there.

Upvotes: 1

Related Questions