ASP.NET IHttpModule IHttpHandler IHttpHandlerFactory 拦截请求

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

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

两端代码的配置如下

  <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 配置的跟本机测试的节点不一样。线上的配置要用

<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

http 转 https 参考链接:https://blog.csdn.net/suxuelian/article/details/80103514

猜你喜欢

转载自www.cnblogs.com/Mrly/p/11497671.html