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查询源数据 ...
随机推荐
- Mac OS 安装 Port
简介 MacPorts类似与apt-get以及yum等软件包管理工具,可以方便的进行安装与卸载软件的功能,同时可以自动安装软件包的依赖,非常方便,同类的还有brew等工具. 安装 下载MacPorts ...
- v4l2简介
V4L是linux内核中关于视频设备的子系统,为linux下的视频驱动提供了统一的接口,使应用程序可以使用统一的API操作不同的视频设备,简化视频系统的开发与维护 V4L2相比与V4L有更好的扩展性和 ...
- Vivado Launching SDK "Importing Hardware Specification" error的解决方法
解决方法是通过参考http://forum.digilentinc.com/topic/611-vivado-launching-sdk-importing-hardware-specificatio ...
- Headfirst设计模式的C++实现——抽象工厂(Abstract Factory)
Dough.h #ifndef _DOUGH_H #define _DOUGH_H class Dough { }; #endif ThinCrustDough.h #ifndef _THIN_CRU ...
- 使用NPOI操作Excel
案例:用NPOI动态生成一个Excel表,然后弹出对话框让用户下载,文件名是"用户列表.xls" 先去相关网站下载 NPOI DLL文件,再引用 application/x-e ...
- HTML教程:link标记
开发php语言的网站,<head>里link标签这样:<link href="xmlrpc.php?rsd=1" title="rsd" ty ...
- OpenCart框架运行流程介绍
框架运行流程介绍 这样的一个get请求http://hostname/index.php?route=common/home 发生了什么? 1. 开始执行入口文件index.php. 2. requi ...
- php中getimagesize函数的用法
php获取图片信息getimagesize,php自带函数.获取图片的类型,尺寸的方法有许多,该函数仅是方法之一. getimagesize() 函数将测定任何 GIF,JPG,PNG,SWF,SWC ...
- sublimeformaya
网上没有找到这样的插件自己造了一个 https://github.com/jonntd/connectionmaya 附件列表
- C语言小结之链表
链表的学习 在数据结构中有一种结构叫做线性表,线性表是储存一个线性数据的表格,本文就简要的介绍一下线性表的构成. 一.线性表的定义定义:由同种类型数据元素构成的有序数列的线性结构长度.表头.表尾Lis ...