C# HttpClient PostAsync with parameters

using System.Net.Http;
 static void Main(string[] args)
        {
            var httpTask = Task<string>.Run<string>(() =>
            {
               return MainAsync();
            });
            httpTask.Wait();   
            Console.WriteLine(httpTask.Result);
            Console.ReadLine();
        }

        static async Task<string> MainAsync()
        {
            using (var client = new HttpClient())
            {                     
                List<KeyValuePair<string, string>> kvpsList = new List<KeyValuePair<string, string>>();
                kvpsList.Add(new KeyValuePair<string, string>("k1", "v1"));
                kvpsList.Add(new KeyValuePair<string, string>("k2", "v2"));
                var formContent = new FormUrlEncodedContent(kvpsList);                
                var result = await client.PostAsync(url, formContent);
                string resultContent = await result.Content.ReadAsStringAsync();
                return resultContent;
            }
        }

 

posted @ 2020-03-24 09:59  FredGrit  阅读(2638)  评论(0编辑  收藏  举报