/// <summary> /// 前端代理处理程序,收和发 /// </summary> /// <param name="context">http上下文</param> /// <returns>结果,不会抛出异常</returns> public static Result <object> Send(HttpContext context) { // 构造一个统计日志 var mainLog = new LogStat() { BusinessType = "http.Proxy" }; mainLog.SetInfo(context); var content = string.Empty; Args <object> a = null; try { content = GetRequestValue(context.Request); var isUdf = IsController(context); if (isUdf) { ApiArgs apiArgs = new ApiArgs { Headers = new Dictionary <string, string>(), Params = new Dictionary <string, string>() }; foreach (var h in context.Request.Headers) { apiArgs.Headers.Add(h.Key, h.Value); } foreach (var p in context.Request.Query) { apiArgs.Params[p.Key] = p.Value; } apiArgs.Body = content; a = new Args <object> { v = apiArgs, ct = ClientType.ThirdPart.ToString() }; } else { a = JsonConvert.DeserializeObject <Args <object> >(content, jSetting); } a.Headers = GetRequestIp(context); if (string.IsNullOrEmpty(a.rid)) { // 提前端产生一个rid a.rid = Guid.NewGuid().ToString("N"); } var rtn = RpcClientManager.Send(a, context.Request.Path); string body = rtn.c; if (isUdf) { if (rtn.r.v != null && !string.IsNullOrEmpty(rtn.r.v.ToString())) { var apiRst = JsonConvert.DeserializeObject <ApiResult>(rtn.r.v.ToString()); if (apiRst != null) { body = apiRst.Body; context.Response.StatusCode = (int)apiRst.Code; if (context.Response.StatusCode < 1) { context.Response.StatusCode = 200; } if (!string.IsNullOrEmpty(apiRst.ContentType)) { context.Response.ContentType = apiRst.ContentType; } } } } using (var strStream = new StreamWriter(context.Response.Body)) { strStream.Write(body); strStream.Flush(); strStream.Close(); } mainLog.SetInfo(a, rtn.r); logger.LogInformation(new EventId(0, a.rid), mainLog.ToString()); return(rtn.r); } catch (Exception e) { Result <object> rr = new Result <object>() { c = 500, msg = e.Message, }; // 处理返回值中的rid,解析请求内容获取rid if (!string.IsNullOrEmpty(content)) { try { a = JsonConvert.DeserializeObject <Args <object> >(content); if (!string.IsNullOrEmpty(a.rid)) { rr.rid = a.rid; } } catch { //反序列化请求内容异常 } } //如果未获取到原始请求rid,则重新生成 if (string.IsNullOrEmpty(rr.rid)) { rr.rid = Guid.NewGuid().ToString("N"); } var jSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; jSetting.DateFormatString = "yyyy-MM-dd HH:mm:ss"; var rst = JsonConvert.SerializeObject(rr, jSetting); using (var strStream = new StreamWriter(context.Response.Body)) { strStream.Write(rst); strStream.Flush(); strStream.Close(); } logger.LogError(new EventId(0, rr.rid), e, $"GrantHttpProxy.Send.Error"); mainLog.SetInfo(null, rr, e); logger.LogInformation(new EventId(0, rr.rid), mainLog.ToString()); return(rr); } }
/// <summary> /// 前端代理处理程序,收和发 /// </summary> /// <param name="context">http上下文</param> /// <returns>结果,不会抛出异常</returns> public static Result <object> Send(HttpContext context) { // 构造一个统计日志 var mainLog = new LogStat() { BusinessType = "http.Proxy" }; mainLog.SetInfo(context); var content = string.Empty; try { content = GetRequestValue(context.Request); Args <object> a = JsonConvert.DeserializeObject <Args <object> >(content, jSetting); a.Headers = GetRequestIp(context); if (string.IsNullOrEmpty(a.rid)) { // 提前端产生一个rid a.rid = Guid.NewGuid().ToString("N"); } var rtn = GrantRpcClientManager.Send(a, context.Request.Path); using (var strStream = new StreamWriter(context.Response.Body)) { strStream.Write(rtn.c); strStream.Flush(); strStream.Close(); } mainLog.SetInfo(a, rtn.r); logger.LogInformation(new EventId(0, a.rid), mainLog.ToString()); return(rtn.r); } catch (Exception e) { Result <object> rr = new Result <object>() { c = 500, msg = e.Message, }; // 处理返回值中的rid,解析请求内容获取rid if (!string.IsNullOrEmpty(content)) { try { Args <object> a = JsonConvert.DeserializeObject <Args <object> >(content); if (!string.IsNullOrEmpty(a.rid)) { rr.rid = a.rid; } } catch { //反序列化请求内容异常 } } //如果未获取到原始请求rid,则重新生成 if (string.IsNullOrEmpty(rr.rid)) { rr.rid = Guid.NewGuid().ToString("N"); } var jSetting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; jSetting.DateFormatString = "yyyy-MM-dd HH:mm:ss"; var rst = JsonConvert.SerializeObject(rr, jSetting); using (var strStream = new StreamWriter(context.Response.Body)) { strStream.Write(rst); strStream.Flush(); strStream.Close(); } logger.LogError(new EventId(0, rr.rid), e, $"GrantHttpProxy.Send.Error"); mainLog.SetInfo(null, rr, e); logger.LogInformation(new EventId(0, rr.rid), mainLog.ToString()); return(rr); } }