专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

ASP.NET IHttpModule IHttpHandler IHttpHandlerFactory 拦截请求

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:51:34       共计:3600 浏览

先来看看代码,拦截所有Http请求类。下面包含了两种类的集成 IHttpModule IHttpHandlerFactory

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 using System; using System.Collections.Generic; using System.Linq; using System.Web;   namespace SoonnetWebSite {     /// <summary>     /// Handler2 的摘要说明     /// </summary>     public class Handler2 : IHttpModule     {         public void Dispose()         {         }           private void Application_AcquireRequestState(Object source, EventArgs e)         {             HttpApplication httpApplication = (HttpApplication)source;             string url = httpApplication.Context.Request.Path.ToLower();             string imgPhysicalPath = httpApplication.Request.Url.ToString();             if (imgPhysicalPath.ToLower().IndexOf("https") != 0 && imgPhysicalPath.IndexOf("Video/VideoUpload.aspx") == -1 && imgPhysicalPath.IndexOf("Photo/PhotoUpload.aspx") == -1)             {                 imgPhysicalPath = imgPhysicalPath.Replace("http", "https");                 httpApplication.Response.Redirect(imgPhysicalPath);                 return;             }             // httpApplication.Response.Redirect(imgPhysicalPath);         }             public void Init(HttpApplication context)         {             context.AcquireRequestState += (new EventHandler(this.Application_AcquireRequestState));         }         //public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)         //{         //    IHttpHandler handler = null;         //    string action = url.Substring(url.LastIndexOf("/", StringComparison.Ordinal) + 1);         //    action = action.Substring(0, action.IndexOf(".", StringComparison.Ordinal));         //    var Rurl = context.Request.RawUrl.Replace("/", ".");         //    string actionClass = $"SoonnetWebSite.{Rurl}";         //    if (true)         //    {           //    }                       //}           //public void ReleaseHandler(IHttpHandler handler)         //{         //    throw new NotImplementedException();         //}     } }


下面我们来看看IHttpHandler 

using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; namespace SoonnetWebSite
{ /// <summary> /// Handler1 的摘要说明 /// </summary> public class Handler1 : IHttpHandler
    { private const string DEFAULTIMAGE_URL = "/SGL_Images/Userlogo_Big.jpg"; public void ProcessRequest(HttpContext context)
        { string imgPhysicalPath = context.Request.Url.ToString(); string rawUrl = context.Request.RawUrl.ToString();
            System.Drawing.Image image = null; if (File.Exists(context.Server.MapPath(rawUrl)))
            { //为空 image = System.Drawing.Image.FromFile(context.Server.MapPath(rawUrl));
            } else { //如果图片不存在,放上默认的图片  image = System.Drawing.Image.FromFile(context.Server.MapPath(DEFAULTIMAGE_URL));
            } //设置输出的类型  context.Response.ContentType = "image/jpeg"; //把图片保存到输出流里   image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            image.Dispose();
        } public bool IsReusable
        { get { return false;
            }
        }
    }
}

两端代码的配置如下

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <system.webServer>   <security>     <requestFiltering>       <requestLimits maxAllowedContentLength="1126400000" />     </requestFiltering>   </security>   <validation validateIntegratedModeConfiguration="false" />   <handlers>     <add name="IHttpHandler" verb="GET,POST" path="LokARX.cc" type="SoonnetWebSite.Web_Data.Lok_ARequestX, SoonnetWebSite" />     <add name="IHttpHandler2" verb="GET" path="LokIF.cc" type="SoonnetWebSite.Web_Data.Lok_Interface, SoonnetWebSite" />     <add name="Handler1" verb="*" path="*.jpg" type="SoonnetWebSite.Handler1, SoonnetWebSite" />   </handlers>   <modules>     <add name="Handler2" type="SoonnetWebSite.Handler2,SoonnetWebSite" />   </modules> </system.webServer>

发现个问题, IIS 配置的跟本机测试的节点不一样。线上的配置要用

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <system.webServer>     <security>       <requestFiltering>         <requestLimits maxAllowedContentLength="1126400000" />       </requestFiltering>     </security>     <validation validateIntegratedModeConfiguration="false" />     <HttpHandlers>       <add name="IHttpHandler" verb="GET,POST" path="LokARX.cc" type="SoonnetWebSite.Web_Data.Lok_ARequestX, SoonnetWebSite" />       <add name="IHttpHandler2" verb="GET" path="LokIF.cc" type="SoonnetWebSite.Web_Data.Lok_Interface, SoonnetWebSite" />       <add name="Handler1" verb="*" path="*.jpg" type="SoonnetWebSite.Handler1, SoonnetWebSite" />     </HttpHandlers>     <HttpModules>       <add name="Handler2" type="SoonnetWebSite.Handler2,SoonnetWebSite" />     </HttpModules>   </system.webServer>

前面配置多了一个http

是因为IIS 的经典模式跟集成模式的关系,

参考链接:https://blog.csdn.net/hongwei_23/article/details/44300923

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:C#代码,功能为拦截http请求,并篡改其中任何内容 | ·下一条:ASP.NET通过HttpModule实现过滤WebService的请求调用修改返回结果

Copyright © 广州京杭网络科技有限公司 2005-2024 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有