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        }
  • 相关阅读:
    推荐一本书 改善你的视力:跟眼镜说再见
    Gentoo中gcc4.1.2到gcc4.3.2的升级
    msbuild学习的一些相关链接
    SqlServer 2005安装问题
    Gentoo linux中安装php5运行环境
    sql 时间函数(全)
    asp.net中的对话框
    win7 资源管理器指向我的电脑
    C/C++ 位操作 总结
    【转】Java字节序转换
  • 原文地址:https://www.cnblogs.com/yanglang/p/9694095.html
Copyright © 2011-2022 走看看