• C# Get、Post、Put、Delete請求WebAPI方法


      1         /// <summary>
      2         /// Get方法
      3         /// </summary>
      4         /// 例如:http://localhost:30202/api/ValuesTest/Sum?num1=1&num2=3
      5         /// <param name="postData">后缀(?num1=1&num2=3)</param>
      6         /// <param name="Url">url(http://localhost:30202/api/ValuesTest/Sum)</param>
      7         /// <returns></returns>
      8         public static string GetInfo(string postData, string Url)
      9         {
     10             try
     11             {
     12                 byte[] byteArray = Encoding.UTF8.GetBytes(postData);
     13                 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
     14                 webRequest.Method = "GET";
     15                 webRequest.Accept = "application/json, text/javascript, */*";  // 出错就删掉
     16                 webRequest.ContentType = "application/json; charset=utf-8";
     17                 webRequest.ContentLength = byteArray.Length;
     18  
     19                 HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
     20                 using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
     21                 {
     22                     return sr.ReadToEnd(); // 返回的数据
     23                 }
     24             }
     25             catch (Exception ex)
     26             {
     27                 throw new Exception(ex.Message);
     28             }
     29         }
     30 
     31         /// <summary>
     32         /// Post请求
     33         /// </summary>
     34         /// <param name="url">URL</param>
     35         /// <param name="body">application/json</param>
     36         /// <returns></returns>
     37         public static string HttpPost(string url, string body)
     38         {
     39             try
     40             {
     41                 byte[] byteArray = Encoding.UTF8.GetBytes(body);
     42                 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
     43                 webRequest.Method = "POST";
     44                 webRequest.Accept = "application/json, text/javascript, */*";
     45                 webRequest.ContentType = "application/json; charset=utf-8";
     46                 webRequest.ContentLength = byteArray.Length;
     47                 webRequest.GetRequestStream().Write(byteArray, 0, byteArray.Length);
     48 
     49                 HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
     50                 using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
     51                 {
     52                     return sr.ReadToEnd();
     53                 }
     54             }
     55             catch (Exception ex)
     56             {
     57                 throw new Exception(ex.Message);
     58             }
     59         }
     60 
     61         /// <summary>
     62         /// Put请求-必有body
     63         /// </summary>
     64         /// <param name="url">URL</param>
     65         /// <param name="body">application/json</param>
     66         /// <returns></returns>
     67         public static string HttpPut(string url, string body)
     68         {
     69             try
     70             {
     71                 byte[] byteArray = Encoding.UTF8.GetBytes(body);
     72                 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
     73                 webRequest.Method = "PUT";
     74                 webRequest.Accept = "application/json, text/javascript, */*";
     75                 webRequest.ContentType = "application/json";
     76                 webRequest.ContentLength = byteArray.Length;
     77                 webRequest.GetRequestStream().Write(byteArray, 0, byteArray.Length);
     78 
     79                 HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
     80                 using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
     81                 {
     82                     return sr.ReadToEnd();
     83                 }
     84             }
     85             catch (Exception ex)
     86             {
     87                 throw new Exception(ex.Message);
     88             }
     89         }
     90 
     91         /// <summary>
     92         /// Delete请求-必有body
     93         /// </summary>
     94         /// <param name="url">URL</param>
     95         /// <param name="body">application/json</param>
     96         /// <returns></returns>
     97         public static string HttpDelete(string url, string body)
     98         {
     99             try
    100             {
    101                 byte[] byteArray = Encoding.UTF8.GetBytes(body);
    102                 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    103                 webRequest.Method = "DELETE";
    104                 webRequest.Accept = "application/json, text/javascript, */*";
    105                 webRequest.ContentType = "application/json";
    106                 webRequest.ContentLength = byteArray.Length;
    107                 webRequest.GetRequestStream().Write(byteArray, 0, byteArray.Length);
    108 
    109                 HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
    110                 using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
    111                 {
    112                     return sr.ReadToEnd();
    113                 }
    114             }
    115             catch (Exception ex)
    116             {
    117                 throw new Exception(ex.Message);
    118             }
    119         }
    365个夜晚,我希望做到两天更一篇博客。加油,小白!
  • 相关阅读:
    hdu1003 Max sum
    Java Programming Tutorial Programming Graphical User Interface (GUI)
    Java中int和String互相转换的多种方法
    Google搜索技巧总结
    HTML 教程延伸阅读:改变文本的外观和含义
    JAR 文件揭密
    sparql 学习,理解sparql
    sparql查询语言学习摘要
    经典算法总结
    杨辉三角
  • 原文地址:https://www.cnblogs.com/qq2806933146xiaobai/p/15397848.html
Copyright © 2020-2023  润新知