HttpModule:经过多个HttpModule,层层处理
HttpHandler:请求的终点,最终的处理
1.首先创建一个网站,然后添加两个ASP.NET页面,并创建一个HttpModule类
namespace WebApplication1
{
public class HttpModelu : IHttpHandler
{
public bool IsReusable
{
get
{
return true;
}
}
public void ProcessRequest(HttpContext context)
{
Uri lastUrl = context.Request.UrlReferrer;
Uri currenUrl = context.Request.Url;
if (lastUrl.Host != currenUrl.Host || lastUrl.Port != currenUrl.Port)
{
string errorImagePath = context.Request.PhysicalApplicationPath + "Error/04.jpg";
context.Response.WriteFile(errorImagePath);
}
else
{
context.Response.WriteFile(context.Request.PhysicalPath);
}
}
}
}
2.配置信息,实现了HttpModule类,还需要在web.confing中配置,在<configuration>节点下添加如下代码:
<system.webServer>
<handlers>
<add verb="*" path="Images/*" type="WebApplication1.HttpModelu" name="plink"/>
</handlers>
</system.webServer>
3.创建站点
1)创建被盗站点。首先创建包含了jpg图片资源的网站,然后添加一个aspx页面,插入本网站提供的三张图片,代码如下:
<div>
<asp:Image ID="Image1" runat="server" ImageUrl="~/Images/01.jpg" />
<asp:Image ID="Image2" runat="server" ImageUrl="~/Images/02.jpg" />
<asp:Image ID="Image3" runat="server" ImageUrl="~/Images/03.jpg" />
</div>
2)创建盗链站点。创建第二个网点,添加aspx页面,引用第一个网站提供的三张图片,代码如下:
<div>
<asp:Image ID="Image1" runat="server" ImageUrl="http://localhost:61014/Images/01.jpg" />
<asp:Image ID="Image2" runat="server" ImageUrl="http://localhost:61014/Images/02.jpg"/>
<asp:Image ID="Image3" runat="server" ImageUrl="http://localhost:61014/Images/03.jpg"/>
</div>