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
- // Does not work
- WebClient wc = new WebClient();
- string postResult = wc.DownloadString( wsUrl );
- Response.ContentType = "text/xml; charset=utf-8";
- Response.Write(postResult);
Expand|Select|Wrap|Line Numbers
- // Works, but its an ugly hack!
- WebClient wc = new WebClient();
- string postResult = wc.DownloadString( wsUrl );
- postResult = postResult.Replace("Ã…", "Å");
- postResult = postResult.Replace("Ø", "Ø");
- postResult = postResult.Replace("ß", "ß");
- Response.ContentType = "text/xml; charset=utf-8";
- Response.Write(postResult);
Expand|Select|Wrap|Line Numbers
- // Does not work either
- WebClient wc = new WebClient();
- wc.Encoding = Encoding.UTF8;
- string postResult = wc.DownloadString( wsUrl );
- Response.ContentType = "text/xml; charset=utf-8";
- Response.Write(postResult);
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 Schøler
Denmark