首页 > Windows开发 > 详细

C# HttpClient PostAsync with parameters

时间:2020-03-24 10:25:55      阅读:209      评论:0      收藏:0      [点我收藏+]
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;
            }
        }

 

C# HttpClient PostAsync with parameters

原文:https://www.cnblogs.com/Fred1987/p/12557113.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!