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");