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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM