2 回答
TA贡献1779条经验 获得超6个赞
我没有意识到,该响应是 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贡献1804条经验 获得超3个赞
我发现缺少请求接受标头的问题!将接受标头设置为收到的响应是行不通的。尝试下面的代码。
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 关注
- 207 浏览
添加回答
举报