zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    markdown with vim
    递归
    类 sizeof
    cppcheck工具
    c++ explicit的含义和用法
    pca主成分分析
    string的使用
    linux的shell进化简史
    adb shell 无法启动 (insufficient permissions for device)
    c++ 四种转换的意思
  • 原文地址:https://www.cnblogs.com/gisoracle/p/14527560.html
Copyright © 2011-2022 走看看