在httpclient C#中添加参数

时间:2019-06-12 13:33:38

标签: c# httpclient

所以我有一个HttpClient,我添加了一些标题。现在我希望一些参数可以在POST请求中使用,但是在使用SendAsync()时很难添加它们; 例如:我想添加参数{“ name”,“ Mary”} 如何添加它们?

我尝试使用FormEncoded,但是我发现我无法使用SendAsync()添加它。

using (var handler = new HttpClientHandler(){CookieContainer = new CookieContainer()} )
            {
                using (var client = new HttpClient(handler)
                    {BaseAddress = new Uri("site.com") })
                {

                    HttpRequestMessage request=new HttpRequestMessage(HttpMethod.Post, "site.com");
                    request.Content.Headers.ContentType= new MediaTypeHeaderValue("application/x-www-form-urlencoded; charset=UTF-8");
                    request.Headers.Add("Upgrade-Insecure-Requests", "1");
                    request.Headers.Add("X-Requested-With", "XMLHttpRequest");

                    request.Headers.Add("X-MicrosoftAjax", "Delta=true");

                    request.Headers.Add("Accept", "*/*");




                    client.Timeout = TimeSpan.FromMilliseconds(10000);
                    var exec = client.SendAsync(request).Result.Content;
                    Console.WriteLine(exec);
                }
            }

0 个答案:

没有答案