昨天粗略讲了一下IHttpHandler接口的作用和动态给图片添加水印的处理,如果对这些不太清除的朋友,建议看看这篇《IHttpHandler的妙用(1):给图片添加水
昨天粗略讲了一下IHttpHandler接口的作用和动态给图片添加水印的处理,如果对这些不太清除的朋友,建议看看这篇《IHttpHandler的妙用(1):给图片添加水印》:http://blog.csdn.net/zhoufoxcn/archive/2008/01/10/2033530.aspx
昨天也提到了IHttpHandler接口主要有一个IsReusable属性和一个ProcessRequest方法,利用这个方法我们可以处理很多事情的,昨天我们利用了这个方法给图片动态添加了水印,今天我再来展示另一种用法。
大家查看一个msdn,可以看到它的声明如下:
Visual Basic(声明)
Sub ProcessRequest ( _
context As HttpContext _
)
Visual Basic(用法)
Dim instance As IHttpHandler
Dim context As HttpContext
instance.ProcessRequest(context)
C#
void ProcessRequest (
HttpContext context
)
注意这个HttpContext对象,它提供对用于为 HTTP 请求提供服务的内部服务器对象(如 Request、Response、Session 和 Server)的引用。
有了它我们就方便多了,因为我们的下载资源一般都会有一个下载介绍(假设为details.aspx?id=***),用户查看介绍之后,如果愿意下载,就会点击下载链接,这个链接也是一个页面(假设为download.aspx?id=***),我们就可以得出结论,只要是用户通过我们的网站下载这些资源,那么在下载资源之前访问那个页面(简称前导页,下同)一定是details.aspx,因此我们就可以得出结论只要是下载之前的前导页不是details.aspx这个页面,那个这个下载请求一定是别的网站盗链(其实还可以放宽一点,在下载之前的前导页一定是本站的页面,也还可以要求更紧一点,下载之前访问的页面的id值一定要与下载的id值一致,这就看大家的实际要求了)!
有了这个推论之后,我们就可以动手写代码了:
using System;
using System.IO;
using System.Web;
///
/// 说明:DownloadHandler是一个防盗链的类,它可以防止本站资源被别的网站盗用
/// 作者:周公
/// 日期:2008-1-11
/// 首发地址:http://blog.csdn.net/zhoufoxcn
///
public class DownloadHandler:IHttpHandler
{
public DownloadHandler()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region IHttpHandler 成员
///
/// 指示IHttpHandler 实例是否可再次使用
///
public bool IsReusable
{
get { return true; }
}
///
/// 处理请求的方法
///
/// 它提供对用于为 HTTP 请求提供服务的内部服务器对象(如 Request、Response、Session 和 Server)的引用。
public void ProcessRequest(HttpContext context)
{
Uri referrerUri = context.Request.UrlReferrer;//获取下载之前访问的那个页面的uri
Uri currentUri = context.Request.Url;
if (referrerUri == null)//没有前导页,直接访问下载页
{
//输出提示,可以根据自身要求完善此处代码
context.Response.Write("请不要盗链本站资源,请从首页访问。首页");
return;
}
#region 判断前导页是否位于本站可以用此段代码
//if (referrerUri.Host == currentUri.Host)//前导页和当前请求页位于同一个主机
//{
// //用户是通过正常路径访问的,向用户提供下载文件
// //实际情况是根据id从数据库找到文件的物理路径,然后输出
// //为了简单代码,仅仅演示流程,这里我直接输出了文件
// //周公注。2008-1-11
// //获取请求的物理文件路径
// WriteFile(context);
//}
//else
//{
// //输出提示,可以根据自身要求完善此处代码
// context.Response.Write("请不要盗链本站资源,请从首页访问。首页");
//}
#endregion
#region 判断前导页是否是我们的介绍页面
string referrerPage = referrerUri.LocalPath.Substring(referrerUri.LocalPath.LastIndexOf('/')+1);
if (referrerPage == "Details.aspx")//如果前导页是我们的介绍页面
{
//用户是通过正常路径访问的,向用户提供下载文件
//实际情况是根据id从数据库找到文件的物理路径,然后输出
//为了简单代码,仅仅演示流程,这里我直接输出了文件
//周公注。2008-1-11
//获取请求的物理文件路径
WriteFile(context);
}
else
{
//输出提示,可以根据自身要求完善此处代码
context.Response.Write("请不要盗链本站资源,请从首页访问。首页");
}
#endregion
}
private void WriteFile(HttpContext context)
{
//用户是通过正常路径访问的,向用户提供下载文件
//实际情况是根据id从数据库找到文件的物理路径,然后输出
//为了简单代码,仅仅演示流程,这里我直接输出了文件
//周公注。2008-1-11
//获取请求的物理文件路径
string path = context.Request.PhysicalPath;
//注意这里rar文件的ContentType是application/octet-stream
//不同格式文件的contentType有可能不同
context.Response.ContentType = "application/octet-stream";
context.Response.WriteFile(path);
}
#endregion
}
xml version="1.0"?>
configuration>
appSettings>
add key="WaterMark" value="http://blog.csdn.net/zhoufoxcn"/>
add key="Font-Size" value="72"/>
appSettings>
connectionStrings/>
system.web>
compilation debug="true"/>
authentication mode="Windows"/>
httpHandlers>
add path="UploadImages/*.jpg" verb="*" type="ImageHandler"/>
add path="*.zip" verb="*" type="DownloadHandler"/>
add path="*.rar" verb="*" type="DownloadHandler"/>
add path="*.iso" verb="*" type="DownloadHandler"/>
httpHandlers>
system.web>
configuration>
@ Page Language="C#" AutoEventWireup="true" CodeFile="Details.aspx.cs" Inherits="Details" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
html xmlns="http://www.w3.org/1999/xhtml" >
head runat="server">
title>无标题页title>
head>
body>
form id="form1" runat="server">
div>
a href="Download.aspx?id=1">下载a>
div>
form>
body>
html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Download : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
CheckUser();
//实际情况是根据id从数据库找到文件的物理路径,然后输出
//为了简单代码,仅仅演示流程,这里我直接输出了文件
//周公注。2008-1-11
//首发地址:http://blog.csdn.net/zhoufoxcn
Response.Redirect("download/demo.rar");//这里就会交给DownloadHanddler处理了
}
}
//检查用户登录等信息
private void CheckUser()
{
//仅仅演示,具体代码根据具体要求编写
//比如用户没有登录如何处理,用户没有下载权限如何处理等
return;
}
}
正常流程截图:
(一)先打开介绍页面:
(二)点击下载链接进入下载页面:
此时出现文件保存对话框,情况正常。
再看盗链情况(非正常情况):
(一)打开首页(非前导页,跳过这一步也没有关系)
(二)直接访问下载页面或者直接访问下载文件的实际url地址:
此时出现了我们自定义的错误提示,甚至地址栏上都出现了该文件的物理地址,可是就是没有办法下载,此时就算是利用迅雷等下载软件,下载到的也是一个无效的文件,如图:
这样就达到我们的目地了,不是通过正常途径来下载是没有办法下载到他需要的文件的。
顺便说一下,写作本文时我是边编写代码边测试的(本博客原创的代码都是如此,测试通过之后方才发表,所以一般不会有什么问题,如果你学习的过程中发现有任何错误,请仔细检查你的代码,因为各人本地环境不同,出错原因各异,恕本人不一一指出你代码的错误之处)。

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software