欢迎光临
我的个人博客网站

C# WebApi 调用

//HttpClient调用帮助类
C#  WebApi 调用C#  WebApi 调用

  public static class HttpRequestHelper     {         #region Get调用         /// <summary>         /// 使用get方法异步请求         /// </summary>         /// <param name="url">目标链接</param>         /// <returns>返回的字符串</returns>         private async static Task<HttpResponseMessage> GetResponseAsync(string url, Dictionary<string, string> header = null, Dictionary<string, string> parame = null)         {             try             {                 HttpClient client = new HttpClient(new HttpClientHandler() { UseCookies = false });                 StringBuilder builder = new StringBuilder();                 builder.Append(url);                 if (header != null)                 {                     client.DefaultRequestHeaders.Clear();                     foreach (var item in header)                     {                         client.DefaultRequestHeaders.Add(item.Key, item.Value);                     }                 }                 if (parame != null && parame.Count > 0)                 {                     builder.Append("?");                     int i = 0;                     foreach (var item in parame)                     {                         if (i > 0)                             builder.Append("&");                         builder.AppendFormat("{0}={1}", item.Key, item.Value);                         i++;                     }                 }                 HttpResponseMessage response = await client.GetAsync(builder.ToString());                 response.EnsureSuccessStatusCode();//用来抛异常的                  return response;             }             catch (Exception e)             {                 //在webapi中要想抛出异常必须这样抛出,否则之抛出一个默认500的异常                 var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError)                 {                     Content = new StringContent(e.ToString()),                     ReasonPhrase = "error"                 };                 throw new HttpResponseException(resp);             }         }          public static async Task<string> GetStringAsync(string url, Dictionary<string, string> header = null, Dictionary<string, string> parame = null)         {             var response = await GetResponseAsync(url, header, parame);             return await response.Content.ReadAsStringAsync();         }          /// <summary>         /// 使用Get返回异步请求返回List集合         /// </summary>         /// <typeparam name="T"></typeparam>          /// <returns></returns>         public static async Task<List<T>> GetListAsync<T>(string url, Dictionary<string, string> header = null, Dictionary<string, string> parame = null)         {             var response = await GetResponseAsync(url, header, parame);             return response.Content.ReadAsAsync<List<T>>().Result;         }          #endregion          #region Post调用         /// <summary>         /// 使用post方法异步请求         /// </summary>         /// <param name="url">目标链接</param>         /// <param name="json">发送的参数字符串-json</param>         /// <returns>返回的字符串</returns>         public static async Task<string> PostAsync(string url, string json, Dictionary<string, string> header = null, bool Gzip = false)         {             HttpClient client = new HttpClient(new HttpClientHandler() { UseCookies = false });             HttpContent content = new StringContent(json);             content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");             if (header != null)             {                 client.DefaultRequestHeaders.Clear();                 foreach (var item in header)                 {                     client.DefaultRequestHeaders.Add(item.Key, item.Value);                 }             }             HttpResponseMessage response = await client.PostAsync(url, content);             response.EnsureSuccessStatusCode();             string responseBody;             if (Gzip)             {                 GZipInputStream inputStream = new GZipInputStream(await response.Content.ReadAsStreamAsync());                 responseBody = new StreamReader(inputStream).ReadToEnd();             }             else             {                 responseBody = await response.Content.ReadAsStringAsync();              }             return responseBody;         }         #endregion          //Put、Delete方式相同      }

View Code

 

 

 

相关链接:相关链接:相关链接:相关链接:相关链接:相关链接:相关链接:相关链接:相关链接:相关链接:相关链接:相关链接:相关链接:相关链接:相关链接:相关链接:相关链接:

https://www.cnblogs.com/liuqiyun/p/9144816.html

https://blog.csdn.net/u011127019/article/details/53021164

https://www.jianshu.com/p/364f1842cae5

https://www.cnblogs.com/guohu/p/12982850.html

赞(0) 打赏
未经允许不得转载:张拓的天空 » C# WebApi 调用
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

专业的IT技术经验分享 更专业 更方便

联系我们本站主机

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏