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;
            }
        }

 

C# HttpClient PostAsync with parameters

上一篇:Core3.1WebApi_ 同源策略_如何支持跨域(转载)


下一篇:c11_api