隐藏

ASP.NET IHttpModule IHttpHandler IHttpHandlerFactory 拦截请求

发布:2021/7/13 16:49:47作者:管理员 来源:本站 浏览次数:1041

先来看看代码,拦截所有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