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

本文关键字:编码 数据 HttpClient POST 请求 | 更新日期: 2023-09-27 18:17:32

我已经编码了想要传递给HttpClient PostRequest字符串的数据

但是FormUrlEncodedContent只接受字典作为参数

我想要一些像客户端。plain_string_content PostAsync (url)

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

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

您可以使用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的捷径。