本文翻译自:How to post data to specific URL using WebClient in C#
I need to use "HTTP Post" with WebClient to post some data to a specific URL I have. 我需要使用WebClient的“HTTP Post”将一些数据发布到我拥有的特定URL。
Now, I know this can be accomplished with WebRequest but for some reasons I wanna use WebClient instead. 现在,我知道这可以通过WebRequest完成,但出于某些原因我想使用WebClient。 Is that possible? 那可能吗? If so, can someone show me some example or point me to the right direction? 如果是这样,有人能告诉我一些例子还是指向正确的方向?
#1楼
参考:https://stackoom.com/question/MfAz/如何使用C-中的WebClient将数据发布到特定URL
#2楼
//Making a POST request using WebClient.
Function()
{
WebClient wc = new WebClient();
var URI = new Uri("http://your_uri_goes_here");
//If any encoding is needed.
wc.Headers["Content-Type"] = "application/x-www-form-urlencoded";
//Or any other encoding type.
//If any key needed
wc.Headers["KEY"] = "Your_Key_Goes_Here";
wc.UploadStringCompleted +=
new UploadStringCompletedEventHandler(wc_UploadStringCompleted);
wc.UploadStringAsync(URI,"POST","Data_To_Be_sent");
}
void wc__UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
try
{
MessageBox.Show(e.Result);
//e.result fetches you the response against your POST request.
}
catch(Exception exc)
{
MessageBox.Show(exc.ToString());
}
}
#3楼
There is a built in method called UploadValues that can send HTTP POST (or any kind of HTTP methods) AND handles the construction of request body (concatenating parameters with "&" and escaping characters by url encoding) in proper form data format: 有一个名为UploadValues的内置方法可以发送HTTP POST(或任何类型的HTTP方法)并以适当的格式数据格式处理请求体的构造(用“&”连接参数和通过url编码转义字符):
using(WebClient client = new WebClient())
{
var reqparm = new System.Collections.Specialized.NameValueCollection();
reqparm.Add("param1", "<any> kinds & of = ? strings");
reqparm.Add("param2", "escaping is already handled");
byte[] responsebytes = client.UploadValues("http://localhost", "POST", reqparm);
string responsebody = Encoding.UTF8.GetString(responsebytes);
}
#4楼
string URI = "http://www.myurl.com/post.php";
string myParameters = "param1=value1¶m2=value2¶m3=value3"
can be simplified as 可以简化为
http://www.myurl.com/post.php?param1=value1¶m2=value2¶m3=value3 . http://www.myurl.com/post.php?param1=value1¶m2=value2¶m3=value3 。
This always works. 这总是有效的。 I found the original one works on and off. 我发现原来的一个上下班。
#5楼
string URI = "site.com/mail.php";
using (WebClient client = new WebClient())
{
System.Collections.Specialized.NameValueCollection postData =
new System.Collections.Specialized.NameValueCollection()
{
{ "to", emailTo },
{ "subject", currentSubject },
{ "body", currentBody }
};
string pagesource = Encoding.UTF8.GetString(client.UploadValues(URI, postData));
}
#6楼
Using WebClient.UploadString
or WebClient.UploadData
you can POST data to the server easily. 使用WebClient.UploadString
或WebClient.UploadData
可以轻松地将数据发送到服务器。 I'll show an example using UploadData, since UploadString is used in the same manner as DownloadString. 我将使用UploadData显示一个示例,因为UploadString的使用方式与DownloadString相同。
byte[] bret = client.UploadData("http://www.website.com/post.php", "POST",
System.Text.Encoding.ASCII.GetBytes("field1=value1&field2=value2") );
string sret = System.Text.Encoding.ASCII.GetString(bret);
more : http://www.daveamenta.com/2008-05/c-webclient-usage/ 更多: http : //www.daveamenta.com/2008-05/c-webclient-usage/