await运算符只能用于异步方法中。请考虑用async修饰符标记此方法,并将其返回类型更改为Task...
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)
转载于:https://www.cnblogs.com/GoZhuang/p/4917856.html