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

后端 未结 7 2379
清酒与你
清酒与你 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: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 :)

提交回复
热议问题