Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c中设置http post请求的正文#_C#_Http - Fatal编程技术网

C# 如何在c中设置http post请求的正文#

C# 如何在c中设置http post请求的正文#,c#,http,C#,Http,一个c#和客户机服务器的新手,希望这不是愚蠢的,但我无法理解如何设置http post请求的主体 我必须使用以下对象来发出请求,我不知道如何将请求主体设置为我必须发送的内容 var request = new HttpRequestMessage(HttpMethod.Post, requestURI); // // Here I believe that I am suppose to set the post request body // var response = httpClient

一个c#和客户机服务器的新手,希望这不是愚蠢的,但我无法理解如何设置http post请求的主体

我必须使用以下对象来发出请求,我不知道如何将请求主体设置为我必须发送的内容

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;

谢谢。非常感谢任何帮助

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

您可以使用如下内容:

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

这是一个post请求,那么为什么不使用
.post
方法呢?