472,982 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,982 software developers and data experts.

C# WebClient UTF-8 woes...


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
Oct 24 '07 #1
2 10017
I fixed this by setting the encoding correctly on the client

wc.Encoding = System.Text.Encoding.UTF8;
Sep 12 '11 #2
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:

Sep 12 '11 #3

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

Similar topics

by: Michael Barrido | last post by:
Greetings! I have a project that needs to access a web service. My problem is this web service is provided via CGI. I cannot do a "Web Reference" to it to add in my dotnet project. I'm planning to...
by: genc ymeri | last post by:
Hi, We are struggeling to upload a file through a C# webClient into JBoss web server. Meanwhile we are able to upload a file from the webserver itself. The problem is only with C# webClient . The...
by: Earl Teigrob | last post by:
I am loading a page using WebClient using the code below. The thing is that on a redirect, I do not end up knowing where the page was loaded from. I need this information because I am parsing the...
by: John A Grandy | last post by:
has anyone successfully used HttpWebRequest or WebClient class to simulate submission of a simple HTML form? for example: a very simple plain-vanilla form with a textbox and a button. when the...
by: Ken | last post by:
We are using the UploadValues function to post a xml string to a java web service. This has been working well for us be we are running into an issue with using Western European characters. The...
by: ddd | last post by:
Hi, I am trying to use webclient to download a file from a windows application and I notice that the application freezes while the file downloads. The file is about 300k so it takes a few...
by: Giggi | last post by:
Hi! I need to pass some strings via an HTTP POST from page1 (in my app) to a page on another server. I stored my data in a NameValueCollection and then sent it using WebClient.UploadValues....
by: jaffar.kazi | last post by:
Hi All, I have a .NET Winforms application that regularly posts to a PHP page. Till a few days back this worked perfectly. However, now, on certain machines, after the data is sent to the server,...
by: MaxMax | last post by:
Is it possible to tell to the WebClient to use an "automatic" encoding when doing DownloadString? The encoding of the connection is written in the header, so the WebClient should be able to sense...
by: Yuriy Galanter | last post by:
Hi, I am using Webclient's method Downloadstring to get a static file from a Webserver by its URL. If the file is in ANSI encoding - I get the string with no problems. If the file is Unicode or...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.