Getting UTF-8 response using httpclient in Windows Store apps

I am creating a Windows Store application, but I am fixated on receiving a UTF-8 response from the 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(); } 

reponseString always contains strange characters that should be like Γ©, and I tried to use a stream, but the API that I found in some examples does not exist on Windows RT.

edit: improved code, another problem

+8
c # utf-8 character-encoding windows-runtime
source share
7 answers

Instead of directly using response.Content.ReadAsStringAsync() you can use response.Content.ReadAsBufferAsync() , which @Kiewic points to, as follows:

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

This works in my case, and I believe that using UTF8 should solve most problems. Now think about why there is no way to do this using ReadAsStringAsync :)

+19
source

I like El Marchewko's approach to using the extension, but the code does not work for me. It happened:

 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(); } } } } 
+2
source

Solved this as follows:

 using (HttpClient client = new HttpClient()) { using (HttpResponseMessage response = client.GetAsync(url).Result) { var byteArray = response.Content.ReadAsByteArrayAsync().Result; var result = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length); return result; } } 
+2
source

HttpClient does not give you much flexibility.

Instead, you can use HttpWebRequest and get the original bytes from the response using HttpWebResponse.GetResponseStream() .

+1
source

I can’t comment, so I’ll have to add my thoughts here.

You can try using _client.GetStringAsync(url) as suggested by @cremor and set authentication headers using the _client.DefaultRequestHeaders property. Alternatively, you can also try using the ReadAsByteArrayAsync method for the response.Content object and use System.Text.Encoding to decode this byte array into a UTF-8 string.

0
source

My approach using the extension:

  using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Windows.Web.Http; namespace yourfancyNamespace { public static class IHttpContentExtension { public static async Task<string> ReadAsStringUTF8Async(this IHttpContent content) { return await content.ReadAsStringAsync(Encoding.UTF8); } public static async Task<string> ReadAsStringAsync(this IHttpContent content, Encoding encoding) { using (TextReader reader = new StreamReader((await content.ReadAsInputStreamAsync()).AsStreamForRead(), encoding)) { return reader.ReadToEnd(); } } } } 
0
source

Perhaps the problem is that the answer is stuck. If the content type is gzip, you will need to expand the response into a string. Some servers do this to conserve bandwidth, which is usually fine. In .NET Core, and possibly in the .NET Framework, this will automatically unpack the response. But this does not work in UWP. This seems like a blatant mistake in UWP.

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

This thread shows a clear example of how to unpack the answer:

Compression / decompression string with C #

0
source

All Articles