Basem Abdelfattah
Basem Abdelfattah

Reputation: 119

Encoding with HttpClient

I am using the HttpClient class in Windows 8. With Windows Phone, I use the WebClient class in combination with encoding to get the right encoding.

WebClient xml = new WebClient();
xml.Encoding = Encoding.GetEncoding("ISO-8859-1");

In Windows 8 it is looks like this:

HttpClient xml = new HttpClient();
HttpResponseMessage response = await xml.GetAsync(uri);                    
responsetext = await response.Content.ReadAsStringAsync();

How can I add a encoding to support German (umlaute)?

Upvotes: 1

Views: 1647

Answers (2)

Hoshinokoe
Hoshinokoe

Reputation: 31

Change ReadAsStringAsync to ReadAsBufferAsync and parse result with required encoding

var buffer = await response.Content.ReadAsBufferAsync();
byte [] rawBytes = new byte[buffer.Length];
using (var reader = DataReader.FromBuffer(buffer))
{
    reader.ReadBytes(rawBytes);
}

var res = Encoding.UTF8.GetString(rawBytes, 0, rawBytes.Length);   

Upvotes: 1

JP Alioto
JP Alioto

Reputation: 45127

I don't have time to test right now, but have you tried using the HttpContent.ReadAsByteArrayAsync method (rather than ReadAsStringAsync) and encoding the resulting byte[] into ISO-8859-1 separately?

Upvotes: 2

Related Questions