C#HttpClient POST请求传递已编码的数据

时间:2015-10-12 18:16:37

标签: c# http

我已经编码了想要传递的数据,就像String传递给HttpClient PostRequest一样

但FormUrlEncodedContent仅接受dictonary作为参数

我想要像client.PostAsync(url,plain_string_content)

这样的东西
 var content = new FormUrlEncodedContent(values);

            using (var client = new HttpClient())
            {

      try
                {

                     var response = client.PostAsync(url, content).GetAwaiter().GetResult();
                     string resp=response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
                     return resp;
                }

1 个答案:

答案 0 :(得分:1)

您可以使用HttpClient.SendAsync

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post);
request.Content = new StringContent(plain_string_content);
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");

await client.SendAsync(request);

请注意,毕竟PostAsync和其他HttpClient方法是SendAsync的快捷方式。