private void button1_Click(object sender, EventArgs e) { string url = "http://localhost:35234/api/Products"; //创建HttpClient(注意传入HttpClientHandler) var handler = new HttpClientHandler() { AutomaticDecompression =System.Net.DecompressionMethods.GZip }; using (HttpClient http = new HttpClient(handler)) { //await异步等待回应 HttpResponseMessage response = await http.GetAsync(url); //确保HTTP成功状态值 response.EnsureSuccessStatusCode(); //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip) Console.WriteLine(await response.Content.ReadAsStringAsync()); } }
出现错误,按提示进行修改
private async void button1_Click(object sender, EventArgs e)