public override void OnException(HttpActionExecutedContext actionExecutedContext) { var code = new HttpResponseMessage(HttpStatusCode.InternalServerError).StatusCode;//设置错误代码:例如:500 404 actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError); string msg = JsonConvert.SerializeObject(new BaseResult() { success = false, message = actionExecutedContext.Exception.Message }); //返回异常错误提示 //写入错误日志相关实现 var portLogEntity = new PortLogEntity { PortName = actionExecutedContext.Request.RequestUri.AbsolutePath, RequestType = actionExecutedContext.Request.Method.ToString(), //StatusCode = Convert.ToInt32(code), ClientIp = GetClientIp(), ParameterList = JsonConvertHelper.ConvertToJson(actionExecutedContext.ActionContext.ActionArguments), Success = false, ErrorMessage = msg }; LogHelper.error("服务器错误信息:\r\n" + JsonConvertHelper.ConvertToJson(portLogEntity)); //result msg = JsonConvertHelper.ConvertToJson( new ResultDataModel <BaseResult> { Code = 4003, Messages = "全局接口异常捕捉到的错误,请联系研发人员处理" }); actionExecutedContext.Response.Content = new StringContent(msg, Encoding.UTF8); }
protected override void HandleUnauthorizedRequest(HttpActionContext actioncontext) { base.HandleUnauthorizedRequest(actioncontext); var response = actioncontext.Response = actioncontext.Response ?? new HttpResponseMessage(); response.StatusCode = HttpStatusCode.OK; var content = JsonConvertHelper.ConvertToJson(new ResultDataModel <NullReferenceException>() { Code = 4002, Messages = "登录令牌失效" }); if (SystemMaintenance.CheckServerMaintain) { content = JsonConvertHelper.ConvertToJson(new ResultDataModel <NullReferenceException>() { Code = 4004, Messages = "服务器正在维护中" }); } //content = AesHelper.AesEncrypt(content, "Jlfc_QQh.2018@11!~^$#GRqB++(())1"); response.Content = new StringContent(content, Encoding.UTF8, "application/json"); }
public static TG PostMsg <TS>(string uri, TS msgJson) { TG modelGet = null; HttpWebResponse response = null; Stream stream = null; try { string msg = JsonConvertHelper.ConvertToJson(msgJson); byte[] msgBytes = GetByteMsg(msg); byte[] htmlBytes = AsHttpAccess.Single.Post(uri, msgBytes); string html; try { //byte[] htmlDecryptBytes = AesHelper.AesDecrypt(htmlBytes, "Jlfc_QQh.2018@11!~^$#GRqB++(())1"); html = _encoding.GetString(htmlBytes); } catch (Exception) { html = ""; } modelGet = JsonConvertHelper.ConvertToObject <TG>(html); } catch (System.Exception) { } finally { if (stream != null) { stream.Close(); } if (response != null) { response.Close(); } } return(modelGet); }