如何使用C#中的WebClient将数据发布到特定URL
本文翻译自: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 neededwc.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/
如何使用C#中的WebClient将数据发布到特定URL相关推荐
- php如何查询数据库,如何在php中查询mysql数据库数据
如何在php中查询mysql数据库数据 发布时间:2020-07-21 09:23:55 来源:亿速云 阅读:81 作者:Leah 本篇文章给大家分享的是有关如何在php中查询mysql数据库数据,小 ...
- python 获取金融数据_class类在python中如何获取金融数据
class类在python中如何获取金融数据 发布时间:2020-12-11 11:12:06 来源:亿速云 阅读:101 作者:小新 这篇文章主要介绍了class类在python中如何获取金融数据, ...
- django 修改mysql_django中怎样修改mysql数据
django中怎样修改mysql数据 发布时间:2020-11-04 11:00:25 来源:亿速云 阅读:74 作者:小新 django中怎样修改mysql数据?这个问题可能是我们日常学习或工作经常 ...
- 计算机位置隐私保护的书,清华大学出版社-图书详情-《隐私保护数据发布:模型与算法》...
隐私保护数据发布: 模型与算法前言 随着数据挖掘和信息共享等数据库应用的出现与发展,如何保护隐私数据和防止敏感信息泄露成为当前面临的重大挑战.作为数据挖掘与信息共享应用中的重要环节,数据发布中的隐私保 ...
- 在公共区块链中通过加密保护数据
链客,专为开发者而生,有问必答! 此文章来自链客区块链技术问答社区,未经允许拒绝转载. 隐私限制 在处理或交换业务文件时,贸易伙伴可能需要某些隐私因素. (1)交易数据的隐私性: 交易数据仅供交易双方 ...
- 如何在Tensorflow.js中处理MNIST图像数据
by Kevin Scott 凯文·斯科特(Kevin Scott) 如何在Tensorflow.js中处理MNIST图像数据 (How to deal with MNIST image data i ...
- Magento中如何在模块中使用多张数据表并配置多个model?
功能介绍: 引用magento开发人员的一句话: Magento has basic one resource to one table resource. 也即是一个资源对应一张数据表. 当有时候, ...
- pandas使用duplicated函数删除dataframe中重复列名称的数据列、默认保留重复数据列中的第一个数据列(removing duplicate columns in dataframe)
pandas使用duplicated函数删除dataframe中重复列名称的数据列.默认保留重复数据列中的第一个数据列(removing duplicate columns in dataframe) ...
- pandas使用query函数基于判断条件获得dataframe中满足条件的数据行(row)的索引列表(index of rows matching conditions in dataframe)
pandas使用query函数基于判断条件获得dataframe中满足条件的数据行(row)的索引列表(index of rows matching conditions in dataframe) ...
最新文章
- Java中数据存储方式
- jquery UI入门
- 十一、jQuery的基本用法
- Qt笔记-Linux程序控制台启动界面实例
- jQuery进阶部分笔记
- 403 forbidden nginx_linux搭建nginx服务
- 10款屏幕取色器/颜色拾取工具软件介绍及下载地址
- android 自定义View【2】对话框取色色盘取色的实现
- orCAD导入AD库 连不上线 更改元件库 出现Unable To Save Part
- 互联网公司常用架构模式梳理
- Android 购物选择颜色、尺码实现
- php怎么做注册短信验证码
- 手机赚钱靠谱的方法,小编分享三个赚钱项目给你!
- 简单的问卷调查发邮件程序
- 84个SEO面试问题
- 电源常识-变压器同名端
- ThinkSNS积分商城系统功能详解!
- HDU6034 Balala Power!
- 打卡机核心功能实现(C语言)
- (私人收藏)蓝色夜空背景的通用商务PPT模板