第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

ReadAsByteArrayAsync 返回不可讀

ReadAsByteArrayAsync 返回不可讀

C#
繁星淼淼 2023-07-09 17:56:33
我正在編寫應(yīng)用程序.NET,我需要從一些 api 獲取數(shù)據(jù)。我嘗試使用不同的閱讀方法,例如ReadAsStringAsync(),我嘗試將它們轉(zhuǎn)換為UTF-8,我設(shè)置 mediaType text/plain ,我嘗試轉(zhuǎn)換為JSON,但在解析過(guò)程中引發(fā)了錯(cuò)誤。HttpClient client = new HttpClient();client.DefaultRequestHeaders.Accept.Clear();byte[] responded;HttpResponseMessage response = await client.GetAsync(path);response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");if (response.IsSuccessStatusCode){    response.Content.ReadAsByteArrayAsync().Wait();    responded =  response.Content.ReadAsByteArrayAsync().Result;    var responseString = Encoding.UTF8.GetString(responded, 0, responded.Length);    Console.WriteLine("\n " +responseString);}我得到回應(yīng):?0E?%?}S??WDJpq?%)X??}???s????A???BK?X?}?k但這不是我所期望的:{"items:[{"has_synonyms":true,"is_moderator_only":false,"is_required":false,"count":9452,"name":"tags"}],"has_more":false,"quota_max":300,"quota_remaining":296}
查看完整描述

2 回答

?
哆啦的時(shí)光機(jī)

TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊

我沒(méi)有意識(shí)到,該響應(yīng)是 gzip 格式。我做了更改:


Stream responded;

HttpResponseMessage response = await client.GetAsync(new Uri(path));

if (response.IsSuccessStatusCode)

{

        response.Content.ReadAsStringAsync().Wait();

        responded = response.Content.ReadAsStreamAsync().Result;

        Stream decompressed = new GZipStream(responded, CompressionMode.Decompress);

        StreamReader objReader = new StreamReader(decompressed, Encoding.UTF8);

        string sLine;

        sLine = objReader.ReadToEnd();

}

并且它工作正常。


查看完整回答
反對(duì) 回復(fù) 2023-07-09
?
狐的傳說(shuō)

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊

我發(fā)現(xiàn)缺少請(qǐng)求接受標(biāo)頭的問(wèn)題!將接受標(biāo)頭設(shè)置為收到的響應(yīng)是行不通的。嘗試下面的代碼。


  HttpClient client = new HttpClient();

  client.DefaultRequestHeaders.Accept.Clear();

  client.DefaultRequestHeaders.Accept.Add(new MediaTypeHeaderValue("application/json"));

  byte[] responded;

  HttpResponseMessage response = await client.GetAsync(path);


  if (response.IsSuccessStatusCode)

  {

        response.Content.ReadAsByteArrayAsync().Wait();

        responded =  response.Content.ReadAsByteArrayAsync().Result;

        var responseString = Encoding.UTF8.GetString(responded, 0, responded.Length);

        Console.WriteLine("\n " +responseString);

  }


查看完整回答
反對(duì) 回復(fù) 2023-07-09
  • 2 回答
  • 0 關(guān)注
  • 275 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)