Link to home
Start Free TrialLog in
Avatar of webressurs
webressursFlag for Norway

asked on

HttpUtility.UrlDecode or unescape in asp.net (c#)

I have a flash that contains links to userprofiles on my asp.net pages. Since I live in Norway some usernames contains some norwegian characters like "æ, ø, å". It the username is "øyvind", the flash links to the profile page like this:

User.aspx?user=%D8yvind

The problem is that my asp.net code don't understand this encoding. If I in asp.net use server.urlencode("øyvind") it looks like this: User.aspx?user=%c3%98yvind. I guess that's why server.urldecode don't understand this querystring parameter.

I have tried so many things, but can't get asp.net to read "%D8yvind" so that it becomes like "øyvind" in my code.

The developers that made the flash say I can use "unescape" to solve this. I have tried this, but no luck when using Request.QueryString["user"]:

string userName = Request.QueryString["user"];  // it don't works when using request.querystring!
//string userName = "%D8yvind";    // this works!

string user = HttpUtility.UrlDecode(userName, System.Text.Encoding.);
Response.Write(user);


Result when reading "user" from querystring is: ¿yvind  (should be: øyvind)


Thanks for all help!!!
Avatar of SprudeVI
SprudeVI
Flag of United States of America image

In unicode, there are several representations for the same glyph, e.g. there is a code for "ø" but the same glyph can be generated with two characters (special "/" + "o") in order to convert from one representation to another you have to "normalize" the string (just google "normalize unicode", and you will find e.g. http://msdn.microsoft.com/en-us/library/dd374126(VS.85).aspx). In .NET you can do something like this:
string name = "øyvind";
name = name.Normalize(NormalizationForm.FormKC;

Open in new window

Avatar of webressurs

ASKER

Thanks for your reply!

Did you mean something like this:

//user.aspx?user=%F8yvind

string userName = Request.QueryString["user"];
string user = userName.Normalize(System.Text.NormalizationForm.FormKC);
user = HttpUtility.UrlDecode(user, System.Text.Encoding.Default);
Response.Write(user);

It still don't works, the result is "¿yvind" (not "øyvind").

Any clue?
Did you try different values from "NormalizationForm"? "FormKC" was the one that I had in my head, but I always mix them up.
Yes, I have tried all the values from NormalizationForm. This is another person with the same problem, but not solved: http://www.velocityreviews.com/forums/t633225-decode-url-string.html

There has to be one way to encode url parameters like this in asp.net C#:
www.website.com?test=%F8%E5%C6%D8%C5
URL encoding should be unicode, but it doesn't have to. Your flash program seems to use the "latin1" encoding (formal name "ISO 8859-1", cf. http://en.wikipedia.org/wiki/ISO/IEC_8859-1).

The following program prints "øåÆØÅ" to the console.

If this is the solution, it would be better to change your flash program to Unicode (e.g. UTF-8) for minimal i18n problems, instead of changing your C# program which assumes unicode by default.
using System;
using System.Text;
using System.Web;

class Test
{
    static void Main(string[] args)
    {
        string username = "%F8%E5%C6%D8%C5";
        Encoding enc = Encoding.GetEncoding("iso-8859-1");
        Console.WriteLine(HttpUtility.UrlDecode(username, enc));
        Console.ReadKey();
    }
}

Open in new window

Use this program to compare all the encodings:
using System;
using System.Text;
using System.Web;

class Test
{
    static void Main(string[] args)
    {
        string username = "%F8yvind";
        foreach (EncodingInfo ei in Encoding.GetEncodings()) {
            Console.WriteLine(ei.Name + ":" + ei.CodePage);
            Encoding enc = ei.GetEncoding();
            Console.WriteLine(HttpUtility.UrlDecode(username, enc));
        }
        Console.ReadKey();
    }
}

Open in new window

Hello, thanks for your reply :)

The thing is that it works if I set the string = %F8yvind directly.
But, if I use Request.Querystring it don't works.

//Works using this:
string userName = "%F8yvind";

//Fails using this:
string userName = Request.QueryString["user"];

System.Text.Encoding enc = System.Text.Encoding.GetEncoding("iso-8859-1");
Response.Write(HttpUtility.UrlDecode(userName, enc));

Hmmm... ? :)
So what is the query string? If you print out Request.QueryString["user"] without decoding it, does the "%F8yvind" arrive, or do you get something else?
Did you try this as well?
string username = Request.QueryString["user"];
foreach (EncodingInfo ei in Encoding.GetEncodings()) {
    Response.Write(ei.Name + ":" + ei.CodePage + ":");
    Encoding enc = ei.GetEncoding();
    Response.Write(HttpUtility.UrlDecode(username, enc) + "<br>");
}

Open in new window

The querystring is like this: test.aspx?user=%F8yvind

If I print out Request.QueryString["user"] without decoding it, "%F8yvind" does not arrive like the querystring. Instead of getting "%F8" I just get a "square" symbol.

When trying the loop, every single decoding has the square symbol instead og "ø".
ASKER CERTIFIED SOLUTION
Avatar of SprudeVI
SprudeVI
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Seems like this is the only way to solve it:

//Get query and removes "user=" from url
string queryUser = Request.ServerVariables["QUERY_STRING"].Substring(5);

//Encoding
string user = HttpUtility.UrlDecode(queryUser, System.Text.Encoding.Default);
           
Response.Write(user);
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial