zoukankan      html  css  js  c++  java
  • 关于C# 怎么调用webapi来获取到json数据

     1        /// <summary>
     2        /// 调用api返回json
     3        /// </summary>
     4        /// <param name="url">api地址</param>
     5        /// <param name="jsonstr">接收参数</param>
     6        /// <param name="type">类型</param>
     7        /// <returns></returns>
     8        public static string HttpApi(string url, string jsonstr, string type)
     9        {
    10            Encoding encoding = Encoding.UTF8;
    11            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址
    12            request.Accept = "text/html,application/xhtml+xml,*/*";
    13            request.ContentType = "application/json";
    14            request.Method = type.ToUpper().ToString();//get或者post
    15            byte[] buffer = encoding.GetBytes(jsonstr);
    16            request.ContentLength = buffer.Length;
    17            request.GetRequestStream().Write(buffer, 0, buffer.Length);
    18            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    19            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
    20            {
    21                return reader.ReadToEnd();
    22            }
    23        }
  • 相关阅读:
    《算法图解》——第六章 广度有限搜索
    《算法图解》——第一章 算法简介
    《算法图解》——第二章 选择排序
    go-json处理的问题
    Go断言
    Go Example--格式化字符串
    Go Example--strings
    Go Example--组合函数
    Go Example--defer
    Go Example--panic
  • 原文地址:https://www.cnblogs.com/yanglang/p/9694095.html
Copyright © 2011-2022 走看看