webressurs
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(user Name, System.Text.Encoding.);
Response.Write(user);
Result when reading "user" from querystring is: ¿yvind (should be: øyvind)
Thanks for all help!!!
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")
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"
//string userName = "%D8yvind"; // this works!
string user = HttpUtility.UrlDecode(user
Response.Write(user);
Result when reading "user" from querystring is: ¿yvind (should be: øyvind)
Thanks for all help!!!
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.Norma lizationFo rm.FormKC) ;
user = HttpUtility.UrlDecode(user , System.Text.Encoding.Defau lt);
Response.Write(user);
It still don't works, the result is "¿yvind" (not "øyvind").
Any clue?
Did you mean something like this:
//user.aspx?user=%F8yvind
string userName = Request.QueryString["user"
string user = userName.Normalize(System.
user = HttpUtility.UrlDecode(user
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.
ASKER
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
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.
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();
}
}
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();
}
}
ASKER
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.GetEn coding("is o-8859-1") ;
Response.Write(HttpUtility .UrlDecode (userName, enc));
Hmmm... ? :)
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.GetEn
Response.Write(HttpUtility
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>");
}
ASKER
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 "ø".
If I print out Request.QueryString["user"
When trying the loop, every single decoding has the square symbol instead og "ø".
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Seems like this is the only way to solve it:
//Get query and removes "user=" from url
string queryUser = Request.ServerVariables["Q UERY_STRIN G"].Substr ing(5);
//Encoding
string user = HttpUtility.UrlDecode(quer yUser, System.Text.Encoding.Defau lt);
Response.Write(user);
//Get query and removes "user=" from url
string queryUser = Request.ServerVariables["Q
//Encoding
string user = HttpUtility.UrlDecode(quer
Response.Write(user);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window