在实际开发中,我们经常会使用到API,所谓API一般就是一个地址,我们称之为接口。然后我们通过用C#对这地址发送请求,请求后,服务器就会给我们返回数据,一般是XML或者JSON,这里我们主要讲述的是JSON。
    为了演示,我们这里准备了一个接口,这是一个查询物流的接口。(读者读到这篇文章的时候,接口可能有效,也可能失效,因为接口是网上找的,不是笔者自己写的,但是原理是一样的。)
    接口: http://www.kuaidi100.com/query?type=快递公司编码&postid=物流单号
    (ps:快递公司编码:申通=”shentong” EMS=”ems” 顺丰=”shunfeng” 圆通=”yuantong” 中通=”zhongtong” 韵达=”yunda” 天天=”tiantian” 汇通=”huitongkuaidi” 全峰=”quanfengkuaidi” 德邦=”debangwuliu” 宅急送=”zhaijisong”)
    一般我们拿到接口后,需要拼接成我们需要的地址。比如,我们现在需要查询顺丰物流的367847964498单的结果。那么,我们就需要拼接这个接口,拼接结果如下:
    http://www.kuaidi100.com/query?type=shunfeng&postid=367847964498
    我们拼接好后,可以直接在浏览器上访问这个地址,看看是不是可以正常访问。如果可以正常访问,说明我们这个接口没有问题。那么,我们现在先在浏览器访问一下。看到下面返回的结果就说明正确。
    C#如何拿到从http上返回JSON数据? - 图1
    接下来就是大家最喜欢的写代码环节,为了方便演示,我们这里用winform程序。非常简单,我们新建一个窗体程序,点击后,弹出JSON数据即可。界面如下:
    C#如何拿到从http上返回JSON数据? - 图2
    建好窗体,放一个按钮,然后我们来创建一个类HttpUitls。这个是这个文章中最重要的。

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Net;
    6. using System.IO;
    7. namespace WindowsFormsApplication1
    8. {
    9. public class HttpUitls
    10. {
    11. public static string Get(string Url)
    12. {
    13. //System.GC.Collect();
    14. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
    15. request.Proxy = null;
    16. request.KeepAlive = false;
    17. request.Method = "GET";
    18. request.ContentType = "application/json; charset=UTF-8";
    19. request.AutomaticDecompression = DecompressionMethods.GZip;
    20. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    21. Stream myResponseStream = response.GetResponseStream();
    22. StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
    23. string retString = myStreamReader.ReadToEnd();
    24. myStreamReader.Close();
    25. myResponseStream.Close();
    26. if (response != null)
    27. {
    28. response.Close();
    29. }
    30. if (request != null)
    31. {
    32. request.Abort();
    33. }
    34. return retString;
    35. }
    36. public static string Post(string Url, string Data, string Referer)
    37. {
    38. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
    39. request.Method = "POST";
    40. request.Referer = Referer;
    41. byte[] bytes = Encoding.UTF8.GetBytes(Data);
    42. request.ContentType = "application/x-www-form-urlencoded";
    43. request.ContentLength = bytes.Length;
    44. Stream myResponseStream = request.GetRequestStream();
    45. myResponseStream.Write(bytes, 0, bytes.Length);
    46. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    47. StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
    48. string retString = myStreamReader.ReadToEnd();
    49. myStreamReader.Close();
    50. myResponseStream.Close();
    51. if (response != null)
    52. {
    53. response.Close();
    54. }
    55. if (request != null)
    56. {
    57. request.Abort();
    58. }
    59. return retString;
    60. }
    61. }
    62. }

    这个类有两个方法,一个是Get,一个是Post,本篇文章我们只需要用到Get就可以了。
    然后是点击按钮的方法

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Windows.Forms;
    9. namespace WindowsFormsApplication1
    10. {
    11. public partial class Form1 : Form
    12. {
    13. public Form1()
    14. {
    15. InitializeComponent();
    16. }
    17. private void button1_Click(object sender, EventArgs e)
    18. {
    19. //我们的接口
    20. string url = "http://www.kuaidi100.com/query?type=shunfeng&postid=367847964498";
    21. //将接口传入,这个HttpUitls的类,有兴趣可以研究下,也可以直接用就可以,不用管如何实现。
    22. string getJson = HttpUitls.Get(url);
    23. MessageBox.Show(getJson);
    24. }
    25. }
    26. }


    然后是运行结果
    C#如何拿到从http上返回JSON数据? - 图3
    到这一步说明我们已经成功拿到接口给我们返回的JSON数据了。那么我们会在下一篇文章中讲解如何使用这JSON数据,也就是解析JSON