加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#从http上拿返回JSON数据

发布时间:2020-12-15 04:45:36 所属栏目:百科 来源:网络整理
导读:h1 class="postTitle"a href="https://www.cnblogs.com/zoujinhua/p/10330037.html"gt;C#如何拿到从http上返回JSON数据? div class="clear"a href="https://www.cnblogs.com/zoujinhua/p/10330037.html"gt;? div class="postBody" div id="cnblogs_post_body

<h1 class="postTitle"><a href="https://www.cnblogs.com/zoujinhua/p/10330037.html"&gt;C#如何拿到从http上返回JSON数据?
<div class="clear"><a href="https://www.cnblogs.com/zoujinhua/p/10330037.html"&gt;?
<div class="postBody">
<div id="cnblogs_post_body" class="blogpost-body">

<h3 class="t">第一章:<a id="post_title_link_7554113" href="https://www.cnblogs.com/zoujinhua/p/10330037.html"&gt;C#如何拿到从http上返回JSON数据?

<h3 class="t">第二章:<a id="post_title_link_7554113" href="https://www.cnblogs.com/zoujinhua/p/10330066.html"&gt;C#如何解析JSON数据?(反序列化对象)

<h3 class="t">第三章:<a id="post_title_link_7554113" href="https://www.cnblogs.com/zoujinhua/p/10330075.html"&gt;C#如何生成JSON字符串?(序列化对象)

<h3 class="t">第四章:<a id="post_title_link_7554113" href="https://www.cnblogs.com/zoujinhua/p/10330084.html"&gt;C#如何生成JSON字符串提交给接口(服务器)

在实际开发中,我们经常会使用到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

我们拼接好后,可以直接在浏览器上访问这个地址,看看是不是可以正常访问。如果可以正常访问,说明我们这个接口没有问题。那么,我们现在先在浏览器访问一下。看到下面返回的结果就说明正确。

接下来就是大家最喜欢的写代码环节,为了方便演示,我们这里用winform程序。非常简单,我们新建一个窗体程序,点击后,弹出JSON数据即可。界面如下:

建好窗体,放一个按钮,然后我们来创建一个类HttpUitls。这个是这个文章中最重要的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace WindowsFormsApplication1
{
public class HttpUitls
{
public static string Get(string Url)
{
//System.GC.Collect();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Proxy = null;
request.KeepAlive = false;
request.Method = "GET";
request.ContentType = "application/json; charset=UTF-8";
request.AutomaticDecompression = DecompressionMethods.GZip;

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream myResponseStream = response.GetResponseStream();
        StreamReader myStreamReader = new StreamReader(myResponseStream,Encoding.UTF8);
        string retString = myStreamReader.ReadToEnd();

        myStreamReader.Close();
        myResponseStream.Close();

        if (response != null)
        {
            response.Close();
        }
        if (request != null)
        {
            request.Abort();
        }

        return retString;
    }

    public static string Post(string Url,string Data,string Referer)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
        request.Method = "POST";
        request.Referer = Referer;
        byte[] bytes = Encoding.UTF8.GetBytes(Data);
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = bytes.Length;
        Stream myResponseStream = request.GetRequestStream();
        myResponseStream.Write(bytes,bytes.Length);

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader myStreamReader = new StreamReader(response.GetResponseStream(),Encoding.UTF8);
        string retString = myStreamReader.ReadToEnd();

        myStreamReader.Close();
        myResponseStream.Close();

        if (response != null)
        {
            response.Close();
        }
        if (request != null)
        {
            request.Abort();
        }
        return retString;
    }

}

}


<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码">

<img src="https://www.52php.cn/res/2019/02-03/17/51e409b11aa51c150090697429a953ed.gif" alt="复制代码">

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

然后是点击按钮的方法

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void button1_Click(object sender,EventArgs e)
    {
        //我们的接口
        string url = "http://www.kuaidi100.com/query?type=shunfeng&amp;postid=367847964498";

        //将接口传入,这个HttpUitls的类,有兴趣可以研究下,也可以直接用就可以,不用管如何实现。
        string getJson = HttpUitls.Get(url);

        MessageBox.Show(getJson);
    }
}

}


<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码">

<img src="https://www.52php.cn/res/2019/02-03/17/51e409b11aa51c150090697429a953ed.gif" alt="复制代码">

?然后是运行结果

到这一步说明我们已经成功拿到接口给我们返回的JSON数据了。那么我们会在下一篇文章中讲解如何使用这JSON数据,也就是解析JSON

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读