URL Decoding Issue --- HELP!

R

Ron Clabo

I have a URL Decoding issue.



The text that needs to be decoded is: "123+H%F6gbergsgatan+st"



The decoded version should equate to: "123 Högbergsgatan st"



This is what I tried:



string encodedText = "123+H%F6gbergsgatan+st";

string decodedText =
System.Web.HttpUtility.UrlDecode(encodedText,System.Text.Encoding.UTF8);



However, it does not work! decodedText ends up being "123 Hgbergsgatan st"
i.e. the ö is omitted.



So to verify that the original text was encoded properly I referenced this
website: http://www.w3schools.com/html/html_ref_urlencode.asp It has a
place where you can paste in a string and click a button to see it's URL
encoded version. Sure enough, the correct url encoding for "123
Högbergsgatan st" is "123+H%F6gbergsgatan+st". As an aside, I also tried
pasting the text into a google search box to see how it got encoded in the
google url. Same encoding. So if it's incoded right, why can't I decode it
in .net?



Still frustrated, I tried using .net to encode the source string (i.e. "123
Högbergsgatan st" to see how it encoded it.



string origText = "123 Högbergsgatan st ";

string encodedText =
System.Web.HttpUtility.UrlEncode(origText,System.Text.Encoding.UTF8);



And low and behold it encoded it different. It encoded it as
"123+H%c3%b6gbergsgatan+st"!!!



I have two questions! How do I get .net to encode it as
"123+H%F6gbergsgatan+st" ? and also when I have a string
"123+H%F6gbergsgatan+st" how do it get .net to decode it as "123
Högbergsgatan st" ?





-Ron
 
G

Gabriel Lozano-Morán

UTF7

string decodedText =
System.Web.HttpUtility.UrlDecode(encodedText,System.Text.Encoding.UTF7);

Gabriel Lozano-Morán
 
R

Ron Clabo

Gabriel - thanks for the reply and the great tip. Using UTF7 does indeed
allow me to decode "123+H%F6gbergsgatan+st" into "123 Högbergsgatan st" !
One problem solved. But unfortunately the reverse is not true. When I try
to use UTF7 to encode "123 Högbergsgatan st" it returns
"123+H%2bAPY-gbergsgatan+st". Ugg. Interestingly using UTF7 to decode this
result does get me back to the original string. Odd that two different
encodings would decode back to the original with UTF7 - I don't get why.

So I still need help on how I encode "123 Högbergsgatan st" into
"123+H%F6gbergsgatan+st". Any thoughts?

-Ron
 
J

Joerg Jooss

Ron said:
I have a URL Decoding issue.



The text that needs to be decoded is: "123+H%F6gbergsgatan+st"



The decoded version should equate to: "123 Högbergsgatan st"
[...]

I have two questions! How do I get .net to encode it as
"123+H%F6gbergsgatan+st" ? and also when I have a string
"123+H%F6gbergsgatan+st" how do it get .net to decode it as "123
Högbergsgatan st" ?

I have no freakin' clue why the other poster wants to use UTF-7 here.
This is ISO-8859-1, which is still widely used for Western European or
North American language web sites.

There's no default ISO-8859-1 instance in System.Text.Encoding, so you
have to create one using
// by IANA name
Encoding enc = Encoding.GetEncoding("ISO-8859-1");
or
// by Windows' internal character set code
Encoding enc = Encoding.GetEncoding(28591);

And since your string is URL encoded as well, you have to use
string str = System.Web.HttpUtility.UrlDecode(encodedStr, enc);

To encode a string, use HttpUtility.UrlEncode().

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top