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)

 

文章来自:http://www.cnblogs.com/GoZhuang/p/4917856.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3