• 自定义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
            }
        }
    

      

  • 相关阅读:
    DRF__序列化(1)serializers.Serializer 基本的序列化、反序列化概念
    小整数池以及字符串驻留机制
    Paginator实现分页
    xml代码 解决eclipse乱码问题
    css,解决文字与图片对齐的问题
    鼠标悬停,背景颜色变化问题
    解决html代码文本复制问题,js技术
    解决 点击超链接后,周围出现的虚线框的问题
    将一个html文件引入另一个html文件的div中
    WebStorm出现中文乱码解决代码
  • 原文地址:https://www.cnblogs.com/xiangxiong/p/7755255.html
Copyright © 2020-2023  润新知