C#通过Url请求路径获取返回的json数据

 对于请求路径获取返回的思路如下:
//1、请求路径 请求方式 请求编码
//2、对请求进行响应 对响应数据转为流   对流数据进行对取  读取的时候以什么方式进行读取  读取为完关闭流  

 如果有参数:对参数进行判断(运用三目运算符)

对于请求路径获取返回的思路如下:
//1、请求路径 请求方式 请求编码
//2、对请求进行响应 对响应数据转为流   对流数据进行对取  读取的时候以什么方式进行读取  读取为完关闭流


/// <summary>
        /// 加载Http请求
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        public static string HttpGet(string Url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return retString;
        }
        /// <summary>
        /// 接口调用方法
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="postDataStr"></param>
        /// <returns></returns>
        public static string HttpGet(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return retString;
        }

 

展开阅读全文

没有更多推荐了,返回首页