• WebApiInvoker


    using System.Data;
    using System.Net.Http;
    using System.Net.Http.Formatting;
    using System.Threading;
    using System.Web.Http;
    using Newtonsoft.Json;
    using OAuthPractice.ProtectedApi;
    
    namespace OAuthPractice.Tests
    {
        public class WebApiInvoker
        {
            private readonly string _apiBaseAddress;
    
            public WebApiInvoker(string apiBaseAddress)
            {
                _apiBaseAddress = apiBaseAddress;
            }
    
            public TResult InvokeGetRequest<TResult>(string api)
            {
                using (var invoker = CreateMessageInvoker())
                {
                    using (var cts = new CancellationTokenSource())
                    {
                        var request = new HttpRequestMessage(HttpMethod.Get, _apiBaseAddress + api);
                        using (HttpResponseMessage response = invoker.SendAsync(request, cts.Token).Result)
                        {
                            if (response.IsSuccessStatusCode)
                            {
                                var result = response.Content.ReadAsStringAsync().Result;
                                return JsonConvert.DeserializeObject<TResult>(result);
                            }
                            throw new HttpResponseException(response);
                        }
                    }
                }
            }
    
            public TResult InvokePostRequest<TResult, TArguemnt>(string api, TArguemnt arg)
            {
                var invoker = CreateMessageInvoker();
                using (var cts = new CancellationTokenSource())
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, _apiBaseAddress + api);
                    request.Content = new ObjectContent<TArguemnt>(arg, new JsonMediaTypeFormatter());
                    using (HttpResponseMessage response = invoker.SendAsync(request, cts.Token).Result)
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            var result = response.Content.ReadAsStringAsync().Result;
                            return JsonConvert.DeserializeObject<TResult>(result);  
                        }
                        throw new HttpResponseException(response);
                    }
                }
            }
    
            private HttpMessageInvoker CreateMessageInvoker()
            {
                var config = new HttpConfiguration();
                WebApiConfig.Register(config);
                var server = new HttpServer(config);
                var messageInvoker = new HttpMessageInvoker(server);
                return messageInvoker;
            }
        }
    }
    

      使用方法:

    var user=new UserModel(){UserName = "richie2",Password = "Password2",ConfirmPassword = "Password2"};
    
                var result = ApiInvoker.InvokePostRequest<IHttpActionResult, UserModel>("account/register", user);
    			result.Should().Should().Equals("ok");
    

      

  • 相关阅读:
    MYSQL数据库实验(存储过程与触发器)
    Markdown
    EXT文件系统
    Arch在VirtualBox虚拟机中挂载U盘
    U盘启动没有引导项
    安装ArchLinux的两篇博文
    Arch Linux上安装Win10
    Gentoo系统安装痕迹化记录
    物联网操作系统安全研究综述
    2013.06_多线程_多核多线程技术综述_眭俊华
  • 原文地址:https://www.cnblogs.com/hbsfgl/p/5288024.html
Copyright © 2020-2023  润新知