与WebClient.DownloadString发送POST在C# [英] Send POST with WebClient.DownloadString in C#
问题描述
我知道有很多关于用C#发送HTTP POST请求的问题,但我正在寻找一个使用方法 Web客户端
,而不是 HttpWebRequest的
。这可能吗?它会是不错的,因为 Web客户端
类是很容易使用。
I know there are a lot of questions about sending HTTP POST requests with C#, but I'm looking for a method that uses WebClient
rather than HttpWebRequest
. Is this possible? It'd be nice because the WebClient
class is so easy to use.
我知道我可以设置标题
属性有一定的头设置,但我不知道是否有可能真正做到从 Web客户端
A POST。
I know I can set the Headers
property to have certain headers set, but I don't know if it's possible to actually do a POST from WebClient
.
推荐答案
您可以使用的 WebClient.UploadData()
它使用HTTP POST,即:
You can use WebClient.UploadData()
which uses HTTP POST, i.e.:
using (WebClient wc = new WebClient())
{
byte[] result = wc.UploadData("http://stackoverflow.com", new byte[] { });
}
您指定的有效载荷数据将作为您的请求的POST主体发送
The payload data that you specify will be transmitted as the POST body of your request.
另外还有 WebClient.UploadValues()
上传名称收藏价值也可以通过HTTP POST。
Alternatively there is WebClient.UploadValues()
to upload a name-value collection also via HTTP POST.
这篇关于与WebClient.DownloadString发送POST在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!