Anti-leeching technology can effectively solve the problems of low website traffic but high pressure. Now we will take the .jpg file as an example to illustrate how to implement anti-leech Technology in Asp.net.
Create an Asp.net website and add two image files: right.jpg, error.jpg, and right.jpg, which are the images shown on the current site, and error.jpg, which are the error images when other websites access the website.
Default. aspx place the following code:
<Asp: Image id = "image1" runat = "server" Height = "179px" imageurl = "~ /Right.jpg "width =" 197px "/>
Create a handler. CS class with the following code:
Using system;
Using system. Web;
Public class handler: ihttphandler {
Public void processrequest (httpcontext context ){
Context. response. expires = 0;
Context. response. Clear ();
Context. response. contenttype = "image/jpg ";
If (context. Request. urlreferrer. Host = "localhost ")
{
Context. response. writefile (context. Request. physicalpath );
Context. response. End ();
}
Else
{
Context. response. writefile (context. Request. physicalapplicationpath + "error.jpg ");
Context. response. End ();
}
}
Public bool isreusable {
Get {
Return true;
}
}
}
Add the following code to the Web. config node:
<Add verb = "*" Path = "*. jpg" type = "handler"/>
Debug the program, and the picture will be displayed as normal.
Give "localhost" in the Code if (context. Request. urlreferrer. Host = "localhost") to another host, and the program will display error.jpg
In this way, we have achieved what we call the image anti-leech technology.
Now, how can this effect be displayed in IIS.
Choose website> right-click Properties> virtual directory> Configuration> ing> Add
Press add to select an executable file:
C: \ windows \ Microsoft. NET \ framework \ v2.0.50727 \ aspnet_isapi.dll
In this way, your website also implements anti-leech technology.