如何在c#中将body设置为http post请求

时间:2015-02-05 23:44:08

标签: c# http

c#和客户端服务器的新手,希望这不是愚蠢的,但我无法理解如何设置http发布请求的正文。

我必须使用以下对象发出请求,我无法理解如何将请求主体设置为我必须发送的内容。

var request = new HttpRequestMessage(HttpMethod.Post, requestURI);
//
// Here I believe that I am suppose to set the post request body
//
var response = httpClient.SendAsync(request).Result;

感谢。任何帮助非常感谢!

2 个答案:

答案 0 :(得分:0)

要设置正文,您需要使用.PostAsync。 GET请求没有身体。

答案 1 :(得分:-1)

您可以使用以下内容:

string xml = String.Empty;
// ... Fill in xml with your data
NameValueCollection parameters = new NameValueCollection();
parameters.Add("data", xml);

byte[] response = null;
string result = String.Empty;

using (WebClient client = new WebClient())
{
    response = client.UploadValues(url, parameters);
    result = Encoding.ASCII.GetString(response);
}
相关问题