• 关于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        }
  • 相关阅读:
    有继承的C++析构函数一定要用virtual
    CUDA vs2010配置
    lambda calculus(1)
    SICP练习1.6 1.16 解答
    用函数式来实现集合
    osx guile编译安装
    skiplist poj2892
    [转]理解 pkgconfig 工具
    专业术语解释
    【转】如何学习linux设备驱动
  • 原文地址:https://www.cnblogs.com/yanglang/p/9694095.html
Copyright © 2020-2023  润新知