• C# 调用webapi


    C#winform程序调用webapi:

    /// <summary>
           /// 调用api返回json
           /// </summary>
           /// <param name="url">api地址</param>
           /// <param name="jsonstr">接收参数</param>
           /// <param name="type">类型</param>
           /// <returns></returns>
           public static string HttpApi(string url, string jsonstr, string type)
           {
               Encoding encoding = Encoding.UTF8;
               HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址
               request.Accept = "text/html,application/xhtml+xml,*/*";
               request.ContentType = "application/json";
               request.Method = type.ToUpper().ToString();//get或者post
               byte[] buffer = encoding.GetBytes(jsonstr);
               request.ContentLength = buffer.Length;
               request.GetRequestStream().Write(buffer, 0, buffer.Length);
               HttpWebResponse response = (HttpWebResponse)request.GetResponse();
               using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
               {
                   return reader.ReadToEnd();
               }
           }
  • 相关阅读:
    堆和栈的区别 (转贴)
    Linux常用的网络命令
    H.264的编解码流程
    链表逆序
    快速排序
    一个计算机硕士毕业生的求职经验(五)
    H.264简单总结
    重要的热键 【Tab】,【Ctrl】—C,【Ctrl】—D
    Linux 文件与目录管理
    一个计算机硕士毕业生的求职经验(六)
  • 原文地址:https://www.cnblogs.com/stilldream/p/10345972.html
Copyright © 2020-2023  润新知