fah*_*976 2 c# encoding webresponse httpclient
我正在尝试使用 HttpClient 从此 URL 获取正确的 JSON 响应。当我在 Chrome 中查看 URL 时,数据是正确格式的 JSON。当我使用 HttpClient 时,我收到一堆看起来像字节或类似内容的垃圾数据。我不知道如何将其解码为字符串。请指教。
string url = "https://api.nasdaq.com/api/calendar/earnings?date=2010-07-30";
string calendar = await DownloadFile(new string[] { url });
private static readonly HttpClient httpClient = new HttpClient();
public static async Task<string> DownloadFile(string[] args)
{
string url = args[0];
httpClient.DefaultRequestHeaders.AcceptEncoding.ParseAdd("gzip, deflate, br");
httpClient.DefaultRequestHeaders.Connection.ParseAdd("keep-alive");
string text = await httpClient.GetStringAsync(url);
return text;
}
Run Code Online (Sandbox Code Playgroud)
数据通过 gzip 压缩返回。您可以HttpClient通过在实例化您的时启用此属性来自动解压缩此数据HttpClient:
private static readonly HttpClient httpClient = new HttpClient(new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
586 次 |
| 最近记录: |