Por*_*les 14 c# post webclient http httpwebrequest
我知道有很多关于用C#发送HTTP POST请求的问题,但我正在寻找一种使用WebClient而不是使用的方法HttpWebRequest.这可能吗?这很好,因为这个WebClient课程很容易使用.
我知道我可以设置Headers属性以设置某些标头,但我不知道是否可以实际执行POST WebClient.
Bro*_*ass 14
您可以使用WebClient.UploadData()哪个使用HTTP POST,即:
using (WebClient wc = new WebClient())
{
byte[] result = wc.UploadData("http://stackoverflow.com", new byte[] { });
}
Run Code Online (Sandbox Code Playgroud)
您指定的有效负载数据将作为请求的POST正文传输.
或者,WebClient.UploadValues()也可以通过HTTP POST上传名称 - 值集合.
您可以使用HTTP 1.0 POST的Upload方法
string postData = Console.ReadLine();
using (System.Net.WebClient wc = new System.Net.WebClient())
{
wc.Headers.Add("Content-Type","application/x-www-form-urlencoded");
// Upload the input string using the HTTP 1.0 POST method.
byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(postData);
byte[] byteResult= wc.UploadData("http://targetwebiste","POST",byteArray);
// Decode and display the result.
Console.WriteLine("\nResult received was {0}",
Encoding.ASCII.GetString(byteResult));
}
Run Code Online (Sandbox Code Playgroud)