C# Sending data using GET or POST ZZ
In this short article, I'll show you how to send data to a website from a C# application using GET or POST method. The tutorial also includes how to receive data from a website, by getting the page's source - so it's a neat way to check if the everything is working as intended.
1. GET Method
Using the GET method is the easiest way to send any
text data since all you have to do is to open the Url address with
already-defined parameters, with WebClient. Notice that WebClient is IDisposable you can use it this way:
- string username = "john";
- string urlAddress = "http://www.yoursite.tld/somepage.php?username=" + username;
- using (WebClient client = new WebClient())
- {
- // this string contains the webpage's source
- string pagesource = client.DownloadString(urlAddress);
- }
The code above opens a Url address, with 1 GET parameter: /somepage.php?username=john.
Now if you need to check what the program sent, use a PHP snippet like this one and look in the source of the page:
- <?php
- $username = $_GET["username"]; //make sure you filter these values, before showing them
- echo $username; //$username == "john"
- ?>
2. POST Method
Sending data using POST, even if it looks similar to GET, you'll need a different approach. Not very different, we're still using WebClient, but we must also include a new class: NameValueCollection. This dictionary-like container will store each parameter's name and value. Once all the data has been loaded, call WebClient.UploadValues to send the information to the webpage.
First, make sure you include this namespace:
- using System.Collections.Specialized;
Then, you can jump to the code:
- string username = "john";
- string referer = "myprogram";
- string urlAddress = "http://www.yoursite.tld/somepage.php";
- using (WebClient client = new WebClient())
- {
- NameValueCollection postData = new NameValueCollection()
- {
- { "username", username }, //order: {"parameter name", "parameter value"}
- { "referer", referer }
- };
- // client.UploadValues returns page's source as byte array (byte[])
- // so it must be transformed into a string
- string pagesource = Encoding.UTF8.GetString(client.UploadValues(urlAddress, postData));
- }
Once again, a short PHP snippet that can be used with the example above
(the result is shown in the source code, downloaded by
WebClient.UploadValues):
- <?php
- $username = $_POST["username"];
- $referer = $_POST["referer"];
- echo $username." from ".$referer; // $username == "john" and $referer == "myprogram"
- ?>
C# Sending data using GET or POST ZZ的更多相关文章
- 1125MySQL Sending data导致查询很慢的问题详细分析
-- 问题1 tablename使用主键索引反而比idx_ref_id慢的原因EXPLAIN SELECT SQL_NO_CACHE COUNT(id) FROM dbname.tbname FORC ...
- mysql索引无效且sending data耗时巨大原因分析
一朋友最近新上线一个项目,本地测试环境跑得好好的,部署到线上却慢得像蜗牛一样.后来查询了一下发现一个sql执行了16秒,有些长的甚至80秒.本地运行都是毫秒级别的查询.下面记录一下困扰了两天的,其中一 ...
- mysql 查询开销 sending data
1.执行一个查询,发现时间开销都在sending data,为什么?2.sending data容易误导,让人以为只是发送数据给客户端,实际上sending data包含两个过程:读取数据并处理,发送 ...
- MySQL Sending data导致查询很慢的问题详细分析【转载】
转自http://blog.csdn.net/yunhua_lee/article/details/8573621 [问题现象] 使用sphinx支持倒排索引,但sphinx从mysql查询源数据的时 ...
- mysql查询sending data占用大量时间的问题处理
问题描述:某条sql语句在测试环境执行只需要1秒不到,到了生产环境执行需要8秒以上 在phpmyadmin里面执行性能分析,发现sending data占用了差不多90%以上的时间 查询一下“Send ...
- 实战:MySQL Sending data导致查询很慢的问题详细分析(转)
这两天帮忙定位一个MySQL查询很慢的问题,定位过程综合各种方法.理论.工具,很有代表性,分享给大家作为新年礼物:) [问题现象] 使用sphinx支持倒排索引,但sphinx从mysql查询源数据的 ...
- 实战:MySQL Sending data导致查询很慢的问题详细分析(转)
出处:http://blog.csdn.net/yunhua_lee/article/details/8573621 这两天帮忙定位一个MySQL查询很慢的问题,定位过程综合各种方法.理论.工具,很有 ...
- MySQL Sending data导致查询很慢的问题详细分析
这两天帮忙定位一个MySQL查询很慢的问题,定位过程综合各种方法.理论.工具,很有代表性,分享给大家作为新年礼物:) [问题现象] 使用sphinx支持倒排索引,但sphinx从mysql查询源数据的 ...
- 0223实战:MySQL Sending data导致查询很慢的问题详细分析
转自博客http://blog.csdn.net/yunhua_lee/article/details/8573621 [问题现象] 使用sphinx支持倒排索引,但sphinx从mysql查询源数据 ...
随机推荐
- swift-07-使用for-in 遍历数组
//for-in /* for 迭代变量 in集合变量 { 使用迭代变量便利所有数据 } */ //遍历数组 var arr = ["a" ,"b" ,&quo ...
- 关于arcgis 9.3破解问题详解
对于初学GIS的同学,安装软件可能会遇到各种各样的问题,对于photoshop,autocad,sketchup,3dmax等软件我们的我们无非是输入特定序列号或者用工具随机生成特定序列号就可以破解, ...
- js和css分别实现透明度的动画实现
js实现 两个函数 即setInterval和setTimeout setTimeout((function(){})(1/10),1*100) 该函数有两个参数,第一个为执行的函数,第二个为事件参数 ...
- LevelDB windows vs2013 c++编译和测试
引用: (src1) :http://download.csdn.net/detail/flyfish1986/8881263(这里有下载地址) (src2) :http://blog.csdn.ne ...
- WebRTC 音视频开发之路
早在2014年就通过WebRTC实现了PC客户端的实时视频语音,那时P2P连接的建立使用的WebRTC自带的libjingle库,使用peerconnection的API实现的.后来在做远程桌面,文件 ...
- 九度OJ 1118 数制转换
题目地址:http://ac.jobdu.com/problem.php?pid=1118 题目描述: 求任意两个不同进制非负整数的转换(2进制-16进制),所给整数在long所能表达的范围之内. ...
- Linux错误码的含义
C Name Value Description EPERM 1 Operation not permitted ENOENT 2 No such file or directory ESRCH 3 ...
- RM-Linux驱动--Watch Dog Timer(看门狗)驱动分析
from:http://blog.csdn.net/geekcome/article/details/6595265 硬件平台:FL2440 内核版本:2.6.28 主机平台:Ubuntu 11,04 ...
- 『奇葩问题集锦』function * (next){ 执行报错 SyntaxError: Unexpected token *
这是因为 app.use(function * (){ 语句中有一个 * ,这种方式被称为generator functions ,一般写作function *(){...} 的形式,在此类func ...
- javascript进阶——面向对象特性
面向对象的javascript是这门语言被设计出来时就考虑的问题,熟悉OOP编程的概念后,学习不同的语言都会发现不同语言的实现是不同的,javascript的面向对象特性与其他具有面向对象特性的语言的 ...