2 回答

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();
}
并且它工作正常。

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);
}
- 2 回答
- 0 關(guān)注
- 275 瀏覽
添加回答
舉報(bào)