Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bad Encoding when receiving response from HttpClient in UWP

I am making uwp(Universal Windows Platform) application and want to deserialize this xml: http://radioa24.info/ramowka.php to object, but I got instead for scpecial characters as ł, ó some strange letters and special ones like: \n and \r: "Ä…"=>"ą" "ć"=>"ć" "Ä™"=>"ę" For example instead of Poniedziałek i got PoniedziaÅ\u0082ek

My code:

var httpClient = new HttpClient();
var response = await httpClient.GetAsync(uri).AsTask();
response.EnsureSuccessStatusCode();
var result = await httpResponse.Content.ReadAsStringAsync();

I was trying to make some Encoding convertions but nothing worked out. How to solve it because later I want to got my object?

var reader = new XmlSerializer(typeof(Sources.Schedule));
using (var tr = new MemoryStream(Encoding.UTF8.GetBytes(resultString)))
{
   Schedule = (Sources.Schedule)reader.Deserialize(res);
}
like image 743
Levvy Avatar asked Jan 27 '16 21:01

Levvy


3 Answers

Please can you try the code below, reading data as bytes solves your issue.

using (HttpClient client = new HttpClient())
{
    Uri url = new Uri("http://radioa24.info/ramowka.php");
    HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Get, url);
    Task<HttpResponseMessage> responseAsync = client.SendRequestAsync(httpRequest).AsTask();
    responseAsync.Wait();
    responseAsync.Result.EnsureSuccessStatusCode();

    Task<IBuffer> asyncBuffer = responseAsync.Result.Content.ReadAsBufferAsync().AsTask();
    asyncBuffer.Wait();
    byte[] resultByteArray = asyncBuffer.Result.ToArray();
    string responseString = Encoding.UTF8.GetString(resultByteArray, 0, resultByteArray.Length);

    responseAsync.Result.Dispose();
}
like image 125
Cihan Uygun Avatar answered Oct 11 '22 10:10

Cihan Uygun


As Jon Skeet notes in his answer this should be fixed on the server. If you check the server sends back the following Content-Type header:

Content-Type: text/xml

It should tell you the encoding (Content-Type: text/xml; charset=utf-8), that's why it should be a server fix.

But if you are sure that this is UTF-8 (it is, because the response xml contains <?xml version="1.0" encoding="UTF-8"?>), you can do this:

var httpClient = new HttpClient();
var response = await httpClient.GetBufferAsync(uri);
var bytes = response.ToArray();
var properEncodedString = Encoding.UTF8.GetString(bytes);
like image 38
Szabolcs Dézsi Avatar answered Oct 11 '22 10:10

Szabolcs Dézsi


Here is my example that works fine with Polish words.

Method to get xml from page:

    public async Task<string> GetXMl(string uri)
    {
        string result = null;
        using (HttpClient httpClient = new HttpClient())
        {
            var response = await httpClient.GetAsync(uri);
            result = await response.Content.ReadAsStringAsync();
        }
        return result;
    }

Method to deserialize xml:

    public void DeserializeXml(string xml)
    {
        var serializer = new XmlSerializer(typeof(ramowka));
        var buffer = Encoding.UTF8.GetBytes(xml);
        using (var stream = new MemoryStream(buffer))
        {
            var ramowka = (ramowka)serializer.Deserialize(stream);
        }
    }

Example how to use methods, for example in button click event:

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        string xml = await GetXMl("http://radioa24.info/ramowka.php");
        DeserializeXml(xml);
    }

Also here you are converted by Visual Studio xml to C# classes http://pastebin.com/aJ4B1aCF

like image 28
matihuf Avatar answered Oct 11 '22 12:10

matihuf