473,498 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# WebClient UTF-8 woes...

3 New Member
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 Schøler
Denmark
Oct 24 '07 #1
2 10122
gcthompson
1 New Member
I fixed this by setting the encoding correctly on the client

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

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

Similar topics

2
938
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...
6
12209
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...
2
1643
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...
14
12101
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...
2
2146
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...
3
2429
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...
3
8352
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....
1
4231
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,...
8
24478
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...
0
1745
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...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7205
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.