Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET encoding url utf8
Hello friends,
 
I need to escape URI data string using ISO-8859-15 encoding.
As I am using this code, the output is still escaped by using UTF-8 encoding, since Uri.EscapeDataString is internally using UTF-8 encoding.
var srcEnc = Encoding.Unicode;
var uniString = "This is a test £.";
var srcBytes = Encoding.Unicode.GetBytes(uniString);
 
var dstEnc = Encoding.GetEncoding("ISO-8859-15"); // http://en.wikipedia.org/wiki/ISO/IEC_8859-15
var dstBytes = Encoding.Convert(srcEnc, dstEnc, srcBytes);
var dstString = dstEnc.GetString(dstBytes);
 
var data = Uri.EscapeDataString(dstString); // output should be "This%20is%20a%20test%20%a3." (%a3 for £)
One workaround would be to take all ISO bytes (dstBytes) and manually format each byte like "%xx", but for debugging reasons this is not acceptable.
I cannot reference System.Web assembly.
 
Thank you for any help.
Posted 19-Mar-13 4:57am
Comments
Sergey Alexandrovich Kryukov at 19-Mar-13 10:01am
   
How come you have this encoding in first place? It is obsolete. Why not to transcode data to Unicode (UTF-8) first?
—SA
Matej Hlatky at 19-Mar-13 10:21am
   
Hi. This encoding is required by some gateway we are using. It's not my choice.
Sergey Alexandrovich Kryukov at 19-Mar-13 10:39am
   
I'm not asking about it. I'm asking why.
—SA
Matej Hlatky at 19-Mar-13 11:29am
   
The 3rd party gateway is used for sending SMS.
When I send escaped utf8 string, while the gateway is expecting escaped string in iso encoding, the incoming SMS contains additional chars.
Gateway additionaly supports sending of utf8 encoded texts (which is the solution in fact), but this can be problem if destination mobile network does not supports
it.
Sergey Alexandrovich Kryukov at 19-Mar-13 12:06pm
   
That is nasty. Anyway, do you have a problem converting to ISO.. ? Unfortunately, I don't know how such think can be used in URI or, say, escaped. If this is a usual, '%' escaping (URL encoding), it should not be a problem.
 
Do you have any correctly working samples of URI for this gateway, with ISO-8859-15 encoding, escaped or whatever. Can you show such sample?
 
—SA
Matej Hlatky at 19-Mar-13 12:12pm
   
Sample escaped iso string is on the last line of my code example in comment ("This%20is%20a%20test%20%a3."). Anyway, I implemented iso encode method by myself.
Thank you for your time.
Sergey Alexandrovich Kryukov at 19-Mar-13 12:15pm
   
This is not a representative sample. How about non-ASCII code points? I answered the question tentatively, please try.
I'll appreciate your feedback. Will it work for you or not.
—SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I cannot be sure that what I suggest will work, because this situation with your gateway looks quite pathological to me.
 
However, I strongly suspect that you simply have to percent-encode all characters beyond ASCII with and pass all characters with ASCII subset (up to code point 127) as is. Just look and compare:
http://en.wikipedia.org/wiki/ISO-8859-15[^],
http://en.wikipedia.org/wiki/ASCII[^].
 
Just for a record: even if you quite redundantly percent-encode each character in the URI, it will work; the only problem is its readability for a user.
 
And the escaping should be done using this schema: http://en.wikipedia.org/wiki/URL_encoding[^].
 
In addition to the characters shown in tables, percent-encode all characters with code points above 127.
 
Just give it a try.
—SA
  Permalink  
Comments
Maciej Los at 19-Mar-13 12:26pm
   
Sergey, i like your comments, for example this: " situation with your gateway looks quite pathological to me".
+5!
Sergey Alexandrovich Kryukov at 19-Mar-13 12:32pm
   
Thank you, Maciej. It looks like we have characteristic Easter European picky attitudes... :-)
I hope my guesswork worked out for OP who accepted it.
—SA
Maciej Los at 19-Mar-13 12:44pm
   
It's not Eastern European picky attitude... Your remarks are totally in of character. As mine ;)
Sergey Alexandrovich Kryukov at 19-Mar-13 13:35pm
   
:-)
Matej Hlatky at 19-Mar-13 12:43pm
   
Thank you again for your time. Your answer is basically the solution which I used and it works like a charm. :)
Sergey Alexandrovich Kryukov at 19-Mar-13 13:35pm
   
My pleasure.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 330
1 Sergey Alexandrovich Kryukov 279
2 BillWoodruff 245
3 Afzaal Ahmad Zeeshan 215
4 CPallini 205
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 19 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100