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查询源数据 ...
随机推荐
- 脱离Xcode,程序在模拟器中无法运行
今天在调试项目的时候 突然发现,如果项目不通过Xcode启动而是直接通过模拟器进行启动,程序闪一下马上退出,并且不是闪退,而是跑到后台去了,并且后台的程序同样无法启动.找了好多解决办法,最后的解决方案 ...
- postgres 利用unique index代替 primay key
create UNIQUE INDEX uniq_index_piwik_log_action_idaction on piwik_log_action(idaction); 这样做的好处: 1. ...
- Mysql笔记【2】-数据表的基本操作
1.创建数据库表 create table <表名> ( 字段名1 类型 <列级别限制> , 字段名2 类型 <列级别限制> , 字段名3 类型 <列级别限制 ...
- Nuage SDN
Nuage推出纯软件解决方案虚拟化业务平台(VSP)由三部分组成:虚拟化业务目录(VSD).虚拟化业务控制器(VSC)和虚拟路由与交换(VRS). VSD是业务/IT策略引擎,可提供业务模板与分析,每 ...
- ubuntu fcitx 安装 使用
系统内置的ibus的86五笔,感觉有些老不太好用, 所以安装试用了一下fcitx下的五笔,记录一下安装方法 ,各种搜索... 我的ubuntn版本: #48-Ubuntu SMP Fri Aug 24 ...
- notepad++使用技巧
1.将tab设置为插入4个空格而不是tab字符 1)点击设置-->首选项 2)选中转换为空格.点击蓝色数字,可以设置制表符宽度 2.对已有文档,进行tab和空格的相互转换 选中编辑-->空 ...
- linux运维工程师,必须掌握以下几个工具
本人是linux运维工程师,对这方面有点心得,现在我说说要掌握哪方面的工具吧说到工具,在行外可以说是技能,在行内我们一般称为工具,就是运维必须要掌握的工具.我就大概列出这几方面,这样入门就基本没问题了 ...
- webstorm快捷方式
刚开始在使用webstrom的时候,不知道快捷方式,感觉自己把webstorm当做记事本使用,真的挺傻的,在朋友的指导下,原来webstorm有快捷方式 一.界面操作 快捷键 说明 ctrl+shif ...
- [转]jquery.timer用法
转自:http://www.cnblogs.com/guohui/archive/2012/02/24/2366668.html 来自JavaEye论坛的JQuery Timers应用知识 jQuer ...
- Cocoa Touch 框架
Cocoa Touch包括: (1)Core Animation: 通过Core Animation,可以通过一个基于独立图层的简单的编程模型来创建丰富的用户体验. (2)Core Audio: Co ...