• 使用HttpClient 调用Web Api


    C#4.5 添加了异步调用Web Api 。

    如果你的项目是4.5以上版本,可以直接参考官方文档。

    http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client

    现在项目使用的是C# 4.0 ,需要对HttpClient 进行修改。

    当然了你要先添加两个引用 Microsoft.Net.Http和Microsoft.AspNet.WebApi.Client

    下面是改造后的HttpClient 

    public class ResultMessage
         {
             public string Code { get; set; }
             public string Message { get; set; }
             public string Content { get; set; }
         }
     
         public class HttpClientHelper
         {
             public static string RequestUrl = "http://localhost:1282/";
     
             /// <summary>
             ///     HttpClient实现Get请求
             /// </summary>
             public static ResultMessage Get(string url)
             {
                 using (var client = new HttpClient())
                 {
                     client.BaseAddress = new Uri(RequestUrl);
                     client.DefaultRequestHeaders.Accept.Clear();
                     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
     
                     Task<HttpResponseMessage> response = client.GetAsync(url);
                     var resultMsg = new ResultMessage();
                     if (response.Result.IsSuccessStatusCode)
                     {
                         Task<string> content = response.Result.Content.ReadAsStringAsync();
                         string resultjson = content.Result;
                         resultMsg = JsonConvert.DeserializeObject<ResultMessage>(resultjson);
                         Console.WriteLine("{0}	{1}	{2}", resultMsg.Code, resultMsg.Message, resultMsg.Content);
                     }
                     else
                     {
                         resultMsg.Code = response.Result.StatusCode.ToString();
                         resultMsg.Message = response.Result.ReasonPhrase;
                         Console.WriteLine("{0} ({1})", (int) response.Result.StatusCode, response.Result.ReasonPhrase);
                     }
     
                     return resultMsg;
                 }
             }
     
             /// <summary>
             ///     HttpClient实现Post请求
             ///     执行服务方法
             /// </summary>
             public static ResultMessage Post(string code, string name)
             {
                 string url = RequestUrl + "api/Biz/Exec";
     
                 using (var client = new HttpClient())
                 {
                     client.BaseAddress = new Uri(RequestUrl);
                     client.DefaultRequestHeaders.Accept.Clear();
                     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
     
                     var content = new FormUrlEncodedContent(new Dictionary<string, string>
                     {
                         {"code",code}
                         {"name",name},
                     });
                     var resultMsg = new ResultMessage();
                     Task<HttpResponseMessage> response = client.PostAsync(url, content);
                     //延时处理
                     if (response.Result.IsSuccessStatusCode)
                     {
                         Task<string> con = response.Result.Content.ReadAsStringAsync();
                         string resultjson = con.Result;
                         resultMsg = JsonConvert.DeserializeObject<ResultMessage>(resultjson);
                     }
                     else
                     {
                         resultMsg.Code = response.Result.StatusCode.ToString();
                         resultMsg.Message = response.Result.ReasonPhrase;
                     }
     
                     return resultMsg;
                 }
             }
         }

      

  • 相关阅读:
    Linux开机启动详解
    git配置多用户多平台
    CentOS7 启动docker.service失败(code=exited, status=1/FAILURE)
    Linux 利用lsof命令恢复删除的文件
    56.storm 之 hello world (集群模式)
    55.storm 之 hello word(本地模式)
    54.Storm环境搭建
    53.storm简介
    深入浅出Mybatis-分页
    storm:最火的流式处理框架
  • 原文地址:https://www.cnblogs.com/bigbrid/p/5590768.html
Copyright © 2020-2023  润新知