Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an UTF-8 response with httpclient in Windows Store apps

I'm building a Windows Store app, but I'm stuck at getting a UTF-8 response from an API.

This is the code:

using (HttpClient client = new HttpClient())
{
    Uri url = new Uri(BaseUrl + "/me/lists");

    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
    request.Headers.Add("Accept", "application/json");
    HttpResponseMessage response = await client.SendRequestAsync(request);
    response.EnsureSuccessStatusCode();

    string responseString = await response.Content.ReadAsStringAsync();

    response.Dispose();
}

The reponseString always contains strange characters which should be accents like é, and I tried using a stream, but the API I found in some examples don't exist in Windows RT.

Edit: improved code, still same problem.

like image 295
Sam Debruyn Avatar asked Mar 26 '14 01:03

Sam Debruyn


3 Answers

Instead of using response.Content.ReadAsStringAsync() directly you could use response.Content.ReadAsBufferAsync() pointed by @Kiewic as follows:

var buffer = await response.Content.ReadAsBufferAsync();
var byteArray = buffer.ToArray();
var responseString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);

This is working in my case and I guess that using UTF8 should solve most of the issues. Now go figure why there is no way to do this using ReadAsStringAsync :)

like image 81
Camilo Martinez Avatar answered Nov 07 '22 10:11

Camilo Martinez


Solved it like this:

using (HttpClient client = new HttpClient())
    {
        using (HttpResponseMessage response = await client.GetAsync(url))
            {
                var byteArray = await response.Content.ReadAsByteArrayAsync();
                var result = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
                return result;
            }
    }
like image 8
Ogglas Avatar answered Nov 07 '22 12:11

Ogglas


I like El Marchewko's approach of using an extension, but the code did not work for me. This did:

using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace WannaSport.Data.Integration
{
    public static class HttpContentExtension
    {
        public static async Task<string> ReadAsStringUTF8Async(this HttpContent content)
        {
            return await content.ReadAsStringAsync(Encoding.UTF8);
        }

        public static async Task<string> ReadAsStringAsync(this HttpContent content, Encoding encoding)
        {
            using (var reader = new StreamReader((await content.ReadAsStreamAsync()), encoding))
            {
                return reader.ReadToEnd();
            }
        }
    }
}
like image 3
pius Avatar answered Nov 07 '22 12:11

pius