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. 【转帖】客户端通过 HTTP 请求和响应 的 Header 信息总结

    请求Header原帖地址:http://technique-digest.iteye.com/blog/1174581 响应Header原帖地址:http://blog.pfan.cn/hurongl ...

  2. PHP学习笔记(八)

    关于PHP中的缓存函数ob_start() and ob_end_flush(). PHP输出机制:输出内容->缓存->输出到浏览器.ob_start(callback function) ...

  3. ZOJ 3471 Most Powerful(DP + 状态压缩)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4257 题目大意:有 n(2<=n<=10) 个原子,每两 ...

  4. Codevs 1427 特种部队(双路DP)

    1427 特种部队 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 黄金 Gold 题目描述 Description 某特种部队接到一个任务,需要潜入一个仓库.该部队士兵分为两路,第一 ...

  5. MySQL的基本

    MySQL的基本语法 left JOIN 左表匹配右表 有没有内容全部匹配 SELECT Persons.LastName, Orders.OrderNo FROM Persons INNER JOI ...

  6. mac 生成支付宝的rsa公钥和私钥 php版本

    openssl genrsa -out rsa_private_key.pem 1024 公钥 openssl rsa -in rsa_private_key.pem -pubout -out rsa ...

  7. hadoop1中hdfs原理详解

    HDFS是Hadoop Distribute File System的简称,也是Hadoop的一个分布四文件系统 一.HDFS的主要设计理念 1.存储超大文件 这里的 “超大文件” 是指几百MB .G ...

  8. Custom template tags and filters

    Code layout Custom template tags and filters must live inside a Django app. If they relate to an exi ...

  9. 整理 iOS 9 适配中出现的坑(图文)

    作者:董铂然 授权本站转载. 本文主要是说一些iOS9适配中出现的坑,如果只是要单纯的了解iOS9新特性可以看瞄神的开发者所需要知道的 iOS 9 SDK 新特性.9月17日凌晨,苹果给用户推送了iO ...

  10. MOS管应用之放反接电路

    一.典型电路 1.电路1 说明: GND-IN 为电源接口的负极 GND 为内部电路的公共地 原理分析 正向接: VCC-IN通过R1.R2.MOS体二极管,最后回到GND-IN;然后GS电压升高,紧 ...