using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Configuration; using System.Diagnostics; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Web; using System.Web.Http; namespace XdCxRhDW.App.WebAPI { /// /// /// [RoutePrefix("")] public class BaseController : ApiController { /// /// /// /// /// /// public static string PostRequest(string url, HttpContent data) { var handler = new HttpClientHandler() { UseCookies = false }; HttpClient client = new HttpClient(handler); client.Timeout = TimeSpan.FromSeconds(10); var message = new HttpRequestMessage(HttpMethod.Post, url); message.Content = data; var response = client.SendAsync(message).Result; response.EnsureSuccessStatusCode(); var result = response.Content.ReadAsStringAsync().Result; return result; } /// /// /// /// /// /// /// public static T PostRequest(string url, HttpContent data) { var handler = new HttpClientHandler() { UseCookies = false }; HttpClient client = new HttpClient(handler); client.Timeout = TimeSpan.FromSeconds(10); var message = new HttpRequestMessage(HttpMethod.Post, url); message.Content = data; var response = client.SendAsync(message).Result; response.EnsureSuccessStatusCode(); var result = response.Content.ReadAsStringAsync().Result; var AjaxResult = JsonConvert.DeserializeObject>(result); if (AjaxResult.code == 200) { return AjaxResult.data; } else { throw new Exception(AjaxResult.msg); } } /// /// Http返回结果 /// public class AjaxResult { /// /// 返回数据 /// public T data { get; set; } /// /// 返回消息 /// public string msg { get; set; } = "ok"; /// /// 状态码.成功=200,失败=0 /// public int code { get; set; } = 200; } /// /// Http返回结果 /// public class AjaxResult { /// /// 返回数据 /// public object data { get; set; } /// /// 返回消息 /// public string msg { get; set; } = "ok"; /// /// 0正确 -1错误 /// public int code { get; set; } = 200; } /// /// 返回成功 /// /// 返回的数据 /// protected AjaxResult Success(object data = null) { AjaxResult res = new AjaxResult { code = 200, msg = "请求成功!", data = data }; return res; } /// /// 返回成功 /// /// 返回的消息 /// 返回的数据 /// protected AjaxResult Success(object data, string msg = "请求成功!") { AjaxResult res = new AjaxResult { code = 200, msg = msg, data = data }; return res; } /// /// 返回成功 /// /// 返回的消息 /// 返回的数据 /// protected AjaxResult Success(T data, string msg = "请求成功!") { AjaxResult res = new AjaxResult { code = 200, msg = msg, data = data }; return res; } /// /// 返回错误 /// /// 错误提示 /// protected AjaxResult Error(string msg) { AjaxResult res = new AjaxResult { code = 0, msg = msg, data = null }; return res; } /// /// 返回错误 /// /// 错误提示 /// protected AjaxResult Error(string msg) { AjaxResult res = new AjaxResult { code = 0, msg = msg, data = default }; return res; } #region WebAPI 获取传统Context /// /// Context /// public HttpContextBase Context { get { return (HttpContextBase)Request.Properties["MS_HttpContext"]; } } /// /// Request /// public HttpRequestBase HttpRequest { get { return Context.Request; } } /// /// Response /// public HttpResponseBase HttpResponse { get { return Context.Response; } } #endregion } }