/// <summary> /// 获取HttpRequest原始信息 /// </summary> /// <param name="request">HttpRequestMessage</param> /// <returns>HttpRequest原始信息</returns> public static HttpRequestRaw ToRaw(this HttpRequestMessage request) { HttpRequestRaw _requestRaw = new HttpRequestRaw(); HttpRequestBasic(request, _requestRaw); HttpRequestHeaders(request, _requestRaw); HttpRequestBody(request, _requestRaw); return(_requestRaw); }
private static void BuilderRequestHeaderKeyValue(HttpHeaders headers, HttpRequestRaw requestRaw) { if (headers != null) { foreach (var item in headers) { requestRaw.Headers.Add(string.Format("{0}:{1}", item.Key, headers.GetValues(item.Key).FirstOrDefault())); } } }
public override void OnActionExceptioning(HttpActionExecutedContext actionExecutedContext, string actionName, HttpStatusCode statusCode, HttpRequestRaw requestRaw) { var response = new HttpResponseMessage { Content = new StringContent("发生故障,请稍后重试!"), StatusCode = statusCode }; actionExecutedContext.Response = response; }
/// <summary> /// Writes the HTTP request raw. /// </summary> /// <param name="httpRequest">The HTTP request.</param> public static void WriteHttpRequestRaw(HttpRequestRaw httpRequest) { if (DebugCurrent) { var debugInfo = _current?.DebugInfo; if (debugInfo != null) { debugInfo.HttpRequestRaw = httpRequest?.ToString(); } } }
/// <summary> /// Creates the HTTP request raw. /// </summary> /// <param name="realm">The realm.</param> /// <param name="version">The version.</param> /// <param name="httpMethod">The HTTP method.</param> /// <param name="resourceName">Name of the resource.</param> /// <param name="resourceAction">The resource action.</param> /// <param name="key">The key.</param> /// <param name="queryString">The query string.</param> /// <returns></returns> protected HttpRequestRaw CreateHttpRequestRaw(string realm, string version, string httpMethod, string resourceName, string resourceAction, string key, Dictionary <string, string> queryString = null) { var result = new HttpRequestRaw { Uri = new Uri(GetRequestUri(Endpoint, realm, version, resourceName, resourceAction, key, queryString)), Method = httpMethod, ProtocolVersion = "1.1", Headers = new System.Collections.Specialized.NameValueCollection { { HttpConstants.HttpHeader.UserAgent, UserAgent } } }; FillAdditionalData(result); return(result); }
/// <summary> /// Fills the additional data. /// </summary> /// <param name="httpRequest">The HTTP request.</param> protected void FillAdditionalData(HttpRequestRaw httpRequest) { if (httpRequest != null) { httpRequest.Headers.AddIfNotNullOrEmpty(HttpConstants.HttpHeader.TOKEN, Token); var currentApiContext = ContextHelper.ApiContext; httpRequest.Headers.AddIfNotNullOrEmpty(HttpConstants.HttpHeader.ORIGINAL, currentApiContext.IpAddress); httpRequest.Headers.AddIfNotNullOrEmpty(HttpConstants.HttpHeader.TRACEID, ApiTraceContext.TraceId); httpRequest.Headers.AddIfNotNullOrEmpty(HttpConstants.HttpHeader.TRACESEQUENCE, ApiTraceContext.TraceSequence.ToString()); httpRequest.Headers.AddIfNotNullOrEmpty(HttpConstants.HttpHeader.OPERATOR, currentApiContext.IndicatedOperator.SafeToString(currentApiContext.CurrentCredential?.Key.ToString())); var userAgent = currentApiContext.UserAgent.SafeToString(); if (!string.IsNullOrWhiteSpace(userAgent)) { httpRequest.Headers.AddIfNotNullOrEmpty(HttpConstants.HttpHeader.UserAgent, userAgent); } } }
private static void HttpRequestBody(HttpRequestMessage request, HttpRequestRaw requestRaw) { try { string _bodyString; using (var stream = request.Content.ReadAsStreamAsync().Result) { if (stream.CanSeek) { stream.Position = 0; } _bodyString = request.Content.ReadAsStringAsync().Result; } requestRaw.Body = _bodyString; } catch { } }
/// <summary> /// 引发异常事件。 /// </summary> /// <param name="actionExecutedContext">操作的上下文。</param> public override void OnException(HttpActionExecutedContext actionExecutedContext) { Exception _exception = actionExecutedContext.Exception; HttpStatusCode _statusCode = HttpStatusCode.OK; if (_exception is KeyNotFoundException || _exception is ArgumentOutOfRangeException) { _statusCode = HttpStatusCode.NotFound; } else if (_exception is ArgumentException) { _statusCode = HttpStatusCode.BadRequest; } else { _statusCode = HttpStatusCode.InternalServerError; } string _actionName = actionExecutedContext.ActionContext.ActionDescriptor.ActionName; HttpRequestRaw _requestRaw = actionExecutedContext.Request.ToRaw(); OnActionExceptioning(actionExecutedContext, _actionName, _statusCode, _requestRaw); }
private static void HttpRequestHeaders(HttpRequestMessage request, HttpRequestRaw requestRaw) { requestRaw.Headers = new List <string>(); BuilderRequestHeaderKeyValue(request.Headers, requestRaw); BuilderRequestHeaderKeyValue(request.Content.Headers, requestRaw); }
private static void HttpRequestBasic(HttpRequestMessage request, HttpRequestRaw requestRaw) { requestRaw.RequestMethod = request.Method.ToString(); requestRaw.RequestUri = request.RequestUri.ToString(); requestRaw.RequestVersion = string.Format("HTTP/{0}", request.Version); }
private static void HttpRequestBasic(HttpRequestMessage request, HttpRequestRaw requestRaw) { requestRaw.RequestMethod = request.Method.ToString(); requestRaw.RequestUri = request.RequestUri.ToString(); requestRaw.RequestVersion = $"HTTP/{request.Version}"; }
/// <summary> /// 当WebApi Action发生异常 /// </summary> /// <param name="actionExecutedContext">HttpActionExecutedContext</param> /// <param name="actionName">WebApi Action名称</param> /// <param name="statusCode">HttpStatusCode</param> /// <param name="requestRaw">Request原始信息</param> public abstract void OnActionExceptioning(HttpActionExecutedContext actionExecutedContext, string actionName, HttpStatusCode statusCode, HttpRequestRaw requestRaw);