zoukankan      html  css  js  c++  java
  • 自定义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
            }
        }
    

      

  • 相关阅读:
    验证码生成程序
    会话技术Cookie&Session
    jsp基础、el技术、jstl标签、javaEE的开发模式
    js原生的Ajax
    java基础-反射
    hibernate 联合主键 composite-id
    在ubuntu18 安装nginx过程,以及遇到的错误!
    报错解决:error: this statement may fall through [-Werror=implicit-fallthrough=]
    nginx.conf中关于nginx-rtmp-module配置指令详解
    搭建Nginx+nginx-rtmp-module的hls流媒体服务器并用OBS进行推流
  • 原文地址:https://www.cnblogs.com/xiangxiong/p/7755255.html
Copyright © 2011-2022 走看看