By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,479 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

C# WebClient UTF-8 woes...

P: 3
Hi,

I'm having problems with the WebClient class regarding UTF-8 encoded data.

When I access a specific webservice directly I can see the data arrives in corretly formatted UTF-8. But when I try to pull data from the same webservice url through the WebClient class I get partly garbled UTF-8 data in return. Only some UTF-8 chars (double byte pairs) seems to be corrupted.

Expand|Select|Wrap|Line Numbers
  1. // Does not work
  2. WebClient wc = new WebClient();
  3. string postResult = wc.DownloadString( wsUrl );
  4. Response.ContentType = "text/xml; charset=utf-8";
  5. Response.Write(postResult);
The above code fails to deliver the data correctly, the below however does the job:

Expand|Select|Wrap|Line Numbers
  1. // Works, but its an ugly hack!
  2. WebClient wc = new WebClient();
  3. string postResult = wc.DownloadString( wsUrl );
  4. postResult = postResult.Replace("Å", "Å");
  5. postResult = postResult.Replace("Ø", "Ø");
  6. postResult = postResult.Replace("ß", "ß");
  7. Response.ContentType = "text/xml; charset=utf-8";
  8. Response.Write(postResult);
Setting the encoding specifically to UTF-8 on the WebClient does not do the trick:

Expand|Select|Wrap|Line Numbers
  1. // Does not work either
  2. WebClient wc = new WebClient();
  3. wc.Encoding = Encoding.UTF8;
  4. string postResult = wc.DownloadString( wsUrl );
  5. Response.ContentType = "text/xml; charset=utf-8";
  6. Response.Write(postResult);
UTF-8 encoded foreign chars such as "", "", "", "", "", ... works great in all of the three code variants above.

My guess is that UTF-8 chars from "0xc3 0xa0" to "0xc3 0xbf" are treated ok by WebClient, whereas chars in the range "0xc3 0x80" to "0xc3 0x9f" are not :-(

Any clue / advice would be greatly appreciated!

Best regards,
Michael Schler
Denmark
Oct 24 '07 #1
Share this Question
Share on Google+
2 Replies


P: 1
I fixed this by setting the encoding correctly on the client

wc.Encoding = System.Text.Encoding.UTF8;
Sep 12 '11 #2

P: 3
October 2007 till September 2011... I guess I won't rate this answer "best". Besides, I answered it (or a similar matter) myself much much earlier:

http://www.xn--schler-dya.net/blog/2...datepanel_bug/
Sep 12 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.