zoukankan      html  css  js  c++  java
  • 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();
               }
           }
  • 相关阅读:
    Delphi常用内存管理函数
    delphi中VirtualStringTree树使用方法
    Delphi常量
    delphi中的copy函数和pos函数
    delphi 中 delete的用法
    字软元件和位软元件的区别
    Virtual Treeview使用要点
    QTreeWidget搜索功能---遍历QTreeWidget
    C 语言使用 sqlite3
    opendir 与 readdir
  • 原文地址:https://www.cnblogs.com/stilldream/p/10345972.html
Copyright © 2011-2022 走看看