博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# Sending data using GET or POST ZZ
阅读量:6221 次
发布时间:2019-06-21

本文共 2445 字,大约阅读时间需要 8 分钟。

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. ?>

转载地址:http://jheja.baihongyu.com/

你可能感兴趣的文章
Spring MVC【入门】就这一篇!
查看>>
windows7 professional.iso
查看>>
postgresql存储过程
查看>>
vue.js的安装部署+cnpm install 安装过程卡住不动----亲测可用
查看>>
如何使用win7自带的备份还原以及创建系统镜像------傻瓜式教程
查看>>
类,接口,抽象类,结构
查看>>
Linux GSO逻辑分析
查看>>
ORACLE 创建表空间
查看>>
keepalived+双主架构
查看>>
robotframwork的WEB功能测试(二)—登录
查看>>
java_有秒计时的数字时钟
查看>>
最小生成树-Prim算法与Kruskal算法
查看>>
阅读笔记11
查看>>
Javascript中大括号“{}”的多义性
查看>>
Loadrunner Get&Post方法性能测试脚本解析
查看>>
Android manifest 获取源代码
查看>>
第三讲课后作业1
查看>>
Studio快捷键
查看>>
弹丸类以及魂类的构想
查看>>
Daily Srum 10.21
查看>>