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
            }
        }
    

      

  • 相关阅读:
    IO流操作-图片操作(二)
    SQL Server 大数据量批量插入
    XSS【跨站脚本攻击】
    Log4net配置
    发布网站碰到的问题
    操作百度API
    Html.BeginForm
    jquery自动识别输入的都是数字
    mysql中随机取出几条数据
    Html中截切文章内容,造成标签不全的问题
  • 原文地址:https://www.cnblogs.com/xiangxiong/p/7755255.html
Copyright © 2011-2022 走看看