• 自定义HTTP消息拦截


    /// <summary>
        /// HTTP消息拦截器
        /// </summary>
        public class RequestHandler : DelegatingHandler
        {
            /// <summary>
            /// 拦截请求
            /// </summary>
            /// <param name="request">请求</param>
            /// <param name="cancellationToken">用于发送取消操作信号</param>
            /// <returns></returns>
            protected async override Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
            {
                //获取URL参数
                NameValueCollection query = HttpUtility.ParseQueryString(request.RequestUri.Query);
                //获取Post正文数据,比如json文本
                string fRequesContent = request.Content.ReadAsStringAsync().Result;
    
                //可以做一些其他安全验证工作,比如Token验证,签名验证。
                //可以在需要时自定义HTTP响应消息
                //return SendError("自定义的HTTP响应消息", HttpStatusCode.OK);
    
                //请求处理耗时跟踪
                Stopwatch sw = new Stopwatch();
                sw.Start();
                //调用内部处理接口,并获取HTTP响应消息
                HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
                //篡改HTTP响应消息正文
                response.Content = new StringContent(response.Content.ReadAsStringAsync().Result.Replace(@"\", @""));
                sw.Stop();
                //记录处理耗时
                long exeMs = sw.ElapsedMilliseconds;
                return response;
            }
    
            /// <summary>
            /// 构造自定义HTTP响应消息
            /// </summary>
            /// <param name="error"></param>
            /// <param name="code"></param>
            /// <returns></returns>
            private HttpResponseMessage SendError(string error, HttpStatusCode code)
            {
                var response = new HttpResponseMessage();
                response.Content = new StringContent(error);
                response.StatusCode = code;
                return response;
            }
        }
    

      

    public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                // Web API 配置和服务
                config.MessageHandlers.Add(new RequestHandler());
                // Web API 路由
                config.MapHttpAttributeRoutes();
            }
        }
    
    public class WebApiApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                GlobalConfiguration.Configure(WebApiConfig.Register);
                GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();//设置返回值统一为json
            }
        }
    

      

  • 相关阅读:
    道路和航线(最短路SPFA优化算法)
    走廊泼水节 (最大生成树)
    大数板子
    强连通分量
    path(CCPC网络赛)
    Plug It In!(网络流板子)
    链式前向星上DFS(Pants On Fire)
    手环定理
    (x+y)%p==b ;(x*y)%p==c; 知道b,c,求x,y;
    牛客多校第六场-H-Pair
  • 原文地址:https://www.cnblogs.com/xiangxiong/p/7755255.html
Copyright © 2020-2023  润新知