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

后端 未结 7 2310
清酒与你
清酒与你 2020-12-10 12:44

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 HttpClien         


        
相关标签:
7条回答
  • 2020-12-10 13:07

    Perhaps the problem is that the response is zipped. If the content type is gzip, you will need decompress the response in to a string. Some servers do this to save bandwidth which is normally fine. In .NET Core and probably .NET Framework, this will automatically unzip the response. But this does not work in UWP. This seems like a glaring bug in UWP to me.

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

    This thread gives a clear example of how to decompress the response:

    Compression/Decompression string with C#

    0 讨论(0)
  • 2020-12-10 13:11

    Solved it like this:

    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;
                }
        }
    
    0 讨论(0)
  • 2020-12-10 13:15

    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 :)

    0 讨论(0)
  • 2020-12-10 13:25

    Can't comment yet, so I'll have to add my thoughts here.

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

    0 讨论(0)
  • 2020-12-10 13:29

    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();
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-10 13:30

    The HttpClient doesn't give you a lot of flexibility.

    You can use a HttpWebRequest instead and get the raw bytes from the response using HttpWebResponse.GetResponseStream().

    0 讨论(0)
提交回复
热议问题