WebClient encoding issue in MonoTouch when building for release

I am having problems with WebClient

that behave differently in release based on real devices than in iOS simulator debug mode.

Apparently the response from the server is interpreted differently and breaks down in releases: Screenshot from the DEBUG build running in the iOS SimulatorScreenshot from the RELEASE build running on an iPhone 4S

This question seems to be an answer to a similar problem, but I don't want to hardcode the encoding in the application, although it may not change in its lifetime, it seems wrong to do this since the information is already contained in HTTP response


I have already opened an issue with Xamarin to investigate this further.


source to share

2 answers

There are two factors here. Firstly:

it seems wrong to do this since the information is already in the HTTP response

Yes, it looks wrong, but Microsoft.NET WebClient.Encoding is identical to System.Text.Encoding.Default by default .

Quoting MSDN:

The default value of this property is the default encoding returned.

As such, Mono (and MonoTouch) implementations WebClient

behave identically . This is often overlooked (works at the time), but it is the source of hard-to-find (not MonoTouch-specific, but .NET-specific) bugs as there is no guarantee of what might be Default


Quoting MSDN:

Different computers may use different default encodings, and the default encoding may even change on the same computer.

Second factor - iOS simulator: simulator not an emulator. This has many advantages (for example, much faster than Android emulators), but it also has its disadvantages (a few IMO, but it only makes them harder to spot).

This means the simulator does not try (hard) to hide the underlying operating system (i.e. OSX) when using a general-purpose API, such as getting the default code page. Since it returns a different value, it System.Text.Encoding.Default

will be initialized with a different code page, which will result in a different implementation being used.

As such, setting your own encoding to not WebClient.Encoding

is the correct (and safe) way to solve your problem (for any .NET application).



I heard a response from the Xamarin support team and they also suggested setting the encoding explicitly WebClient


It WebClient.Encoding

was installed on my device System.Text.Encoding.ASCII

instead of the UTF8

one used in iOS Simulator


Still not a general solution, but at least you can use the high-level one DownloadString


using (var client = new WebClient())
    client.Encoding = System.Text.Encoding.UTF8;
    var response = client.DownloadString("http://dl.dropbox.com/u/58977881/umlautTest.txt");




All Articles