文章内容

2017/8/5 17:47:47,作 者: 黄兵

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)

本文转载自:zhuang75 - await运算符只能用于异步方法中。请考虑用async修饰符标记此方法,并将其返回类型更改为Task

分享到:

发表评论

评论列表