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

后端 未结 7 2383
清酒与你
清酒与你 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: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;
                }
        }
    

提交回复
热议问题