• C#从http上拿返回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, 0, 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;
            }
        
        }
    }

    这个类有两个方法,一个是Get,一个是Post

    获取json对象

    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&postid=367847964498";
    
                //将接口传入,这个HttpUitls的类,有兴趣可以研究下,也可以直接用就可以,不用管如何实现。
                string getJson = HttpUitls.Get(url);
    
                MessageBox.Show(getJson);
            }
        }
    }

    原文来自于:https://www.cnblogs.com/zoujinhua/p/10330037.html

  • 相关阅读:
    【转】Android Activity类应用技巧分享
    【转】LinearLayout:
    正则表达式语法
    【转】cocos2dx 详细配置!(一款C++编写的多平台2D游戏开源框架)
    子网掩码与子网的划分
    防黑阻击入侵检测之蜜罐与蜜网
    手机停机照样免费上网(无限量)
    【转】QQ邮箱挂吗让你肉鸡满天飞【提醒】
    【转】记事本也能杀毒,处理顽固程序
    [转载]虚拟的东西也会枯竭——全球IPv4地址明年初将用完
  • 原文地址:https://www.cnblogs.com/xiong950413/p/15337778.html
Copyright © 2020-2023  润新知