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:

  1. string username = "john";
  2. string urlAddress = "http://www.yoursite.tld/somepage.php?username=" + username;
  3. using (WebClient client = new WebClient())
  4. {
  5. // this string contains the webpage's source
  6. string pagesource = client.DownloadString(urlAddress);
  7. }

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:

  1. <?php
  2. $username = $_GET["username"]; //make sure you filter these values, before showing them
  3. echo $username; //$username == "john"
  4. ?>

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:

  1. using System.Collections.Specialized;

Then, you can jump to the code:

  1. string username = "john";
  2. string referer = "myprogram";
  3. string urlAddress = "http://www.yoursite.tld/somepage.php";
  4. using (WebClient client = new WebClient())
  5. {
  6. NameValueCollection postData = new NameValueCollection()
  7. {
  8. { "username", username }, //order: {"parameter name", "parameter value"}
  9. { "referer", referer }
  10. };
  11. // client.UploadValues returns page's source as byte array (byte[])
  12. // so it must be transformed into a string
  13. string pagesource = Encoding.UTF8.GetString(client.UploadValues(urlAddress, postData));
  14. }

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):

    1. <?php
    2. $username = $_POST["username"];
    3. $referer = $_POST["referer"];
    4. echo $username." from ".$referer; // $username == "john" and $referer == "myprogram"
    5. ?>

C# Sending data using GET or POST ZZ的更多相关文章

  1. 1125MySQL Sending data导致查询很慢的问题详细分析

    -- 问题1 tablename使用主键索引反而比idx_ref_id慢的原因EXPLAIN SELECT SQL_NO_CACHE COUNT(id) FROM dbname.tbname FORC ...

  2. mysql索引无效且sending data耗时巨大原因分析

    一朋友最近新上线一个项目,本地测试环境跑得好好的,部署到线上却慢得像蜗牛一样.后来查询了一下发现一个sql执行了16秒,有些长的甚至80秒.本地运行都是毫秒级别的查询.下面记录一下困扰了两天的,其中一 ...

  3. mysql 查询开销 sending data

    1.执行一个查询,发现时间开销都在sending data,为什么?2.sending data容易误导,让人以为只是发送数据给客户端,实际上sending data包含两个过程:读取数据并处理,发送 ...

  4. MySQL Sending data导致查询很慢的问题详细分析【转载】

    转自http://blog.csdn.net/yunhua_lee/article/details/8573621 [问题现象] 使用sphinx支持倒排索引,但sphinx从mysql查询源数据的时 ...

  5. mysql查询sending data占用大量时间的问题处理

    问题描述:某条sql语句在测试环境执行只需要1秒不到,到了生产环境执行需要8秒以上 在phpmyadmin里面执行性能分析,发现sending data占用了差不多90%以上的时间 查询一下“Send ...

  6. 实战:MySQL Sending data导致查询很慢的问题详细分析(转)

    这两天帮忙定位一个MySQL查询很慢的问题,定位过程综合各种方法.理论.工具,很有代表性,分享给大家作为新年礼物:) [问题现象] 使用sphinx支持倒排索引,但sphinx从mysql查询源数据的 ...

  7. 实战:MySQL Sending data导致查询很慢的问题详细分析(转)

    出处:http://blog.csdn.net/yunhua_lee/article/details/8573621 这两天帮忙定位一个MySQL查询很慢的问题,定位过程综合各种方法.理论.工具,很有 ...

  8. MySQL Sending data导致查询很慢的问题详细分析

    这两天帮忙定位一个MySQL查询很慢的问题,定位过程综合各种方法.理论.工具,很有代表性,分享给大家作为新年礼物:) [问题现象] 使用sphinx支持倒排索引,但sphinx从mysql查询源数据的 ...

  9. 0223实战:MySQL Sending data导致查询很慢的问题详细分析

    转自博客http://blog.csdn.net/yunhua_lee/article/details/8573621 [问题现象] 使用sphinx支持倒排索引,但sphinx从mysql查询源数据 ...

随机推荐

  1. 最近在学习UDP方面的通信,找到一个很棒的博客

    http://blog.csdn.net/kesalin/article/details/8798039

  2. javaScript笔记1

    一.通过 id 访问HTML元素,可以使用 document.getElementById(id) 方法. 例子: <body> <button id="mybtn&quo ...

  3. error signing assembly unknown error

    用VS2010 编译 C#工程,出现 Cryptographic failure while signing assembly 'Assembly.dll' -- 'Unknown error (80 ...

  4. AJAX原理及优缺点

    1.ajax技术的背景 不可否认,ajax技术的流行得益于google的大力推广,正是由于google earth.google suggest以及gmail等对ajax技术的广泛应用,催生了ajax ...

  5. IOS 学习笔记 2015-04-09 0C-SQLite 数据存储

    1 项目导入 libsqlite3.0.dylib 框架 2 在使用数据sqlite的头文件(.h)上导入 #Impourt <sqlite3.h> 3 推荐自己本地电脑下个sqlite ...

  6. 快速排序 javascript实现

    Quicksort(快速排序) 是由 东尼·霍尔 所发展的一种排序. 它比其他的Ο(n log n)算法更快, 因为它的内部循环(inner loop)可以在大部分的架构上很有效率地被实现出来.当然, ...

  7. Mysql备份数据库的一种方法

    今天添加了一个数据库自动备份的模块,mysql数据备份的方法有很多,可以对单个数据库备份,可以多个数据库备份,也可以对某一个表进行备份,可以只备份数据库的结构不备份数据,可以根据需要做不同处理,正好现 ...

  8. linux磁盘以及文件系统

    df 查看磁盘总容量 -i 显示inodes号 -h 使用合适的单位显示磁盘大小 -m 以M为单位显示 -k 以K为单位显示 默认K显示 du 用来查看某个目录或者文件所占空间大小 参数:-abckm ...

  9. JavaScript学习总结【11】、JS 运动

    动画效果在网站中是一种非常常见的交互式体验效果,比如侧边栏分享.图片淡入淡出,我们把这种动画效果就叫做运动,也就是让物体动起来.如果想让一个物体动起来,无非就是改变它的速度,也就是改变属性值,比如 l ...

  10. js 不可变的原始值和可变的对象引用

    javascript中的原始值(undefined.null.布尔值.数字和字符串)与对象(包括数组和函数)有着根本区别.原始值是不可更改的:任何方法都无法更改(或“突变”)一个原始值.对数字和布尔值 ...