zoukankan      html  css  js  c++  java
  • c# 请求api获得json数据

        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;
            }
        }
    string json = HttpUitls.Post("http://music.163.com/api/search/pc", string.Format("s={0}&offset={1}&limit={2}&type={3}", key, "0", "10", "1"), "http://music.163.com/");
  • 相关阅读:
    swiper-wrapper轮滑组件(多组轮滑界面)间隔无效问题
    jquery .play()报错is not a function
    html 表单input disabled属性提交后台无法获得数据
    git报错:Auto Merge Failed; Fix Conflicts and Then Commit
    js 获取转换网址中文参数
    day36 作业
    day38 并发编程(理论)
    day35 作业
    day36 解决粘包问题
    day35 socket套接字介绍
  • 原文地址:https://www.cnblogs.com/margin-gu/p/5887509.html
Copyright © 2011-2022 走看看