Prevent image leeching-ASP. NET Technology

Source: Internet
Author: User

1. What is image leeching?

Let's analyze the general browsing phenomenon first. The most important point is that a complete page is not transmitted to the client all at once. if you are requesting a page with many images and other information, the first HTTP request is sent back the text of this page, then explain the text in the browser of the client and find that there are images in the text. Then, the browser of the client will send an HTTP request, after the request is processed, the image file will be sent to the client, and then the browser places the image back to the correct position on the page, A complete page may be displayed only after multiple HTTP requests are sent. based on this mechanism, a problem occurs, that is, leeching: If a website does not contain the information in the page, it can connect the image to other websites. in this way, websites without any resources use other website resources to present the resources to the viewers, which increases their access traffic, and most viewers will not easily find it. Obviously, it is unfair for the website that has been used resources.

2 solution

Now, Asp. the httphandler in net can solve this problem well. this is because by default, we only process dynamic web pages, such as ASP and Aspx. However, when an image file is requested, IIS directly extracts resources and sends them to the client, it seems a little blind.

Therefore, we need to create our own httphandler to process image files. For example, JPG files.

3. How can we create our own httphandler and register it in a web application?

(1) Build your own httphandler

Create a class that inherits the system. Web. ihttphandler interface. The system. Web. ihttphandler interface has only two members.

1. isreusable attribute. The returned value indicates whether other HTTP requests can use the instance that currently inherits the system. Web. ihttphander interface class.

2 processrequest (system. Web. httpcontext context) method, except for special HTTP requests that are required to be processed in user-defined processes.

The instance of the system. Web. httpcontext class contains all the information required by the HTTP protocol in an HTTP request. System. web. the httpcontext class contains a property request that allows the value of the HTTP request information sent from the client to be easily read; the property response encapsulates the information and operations to be returned to the client. Of course there are still many common attributes and methods, so we will not detail them here. We only use these two attributes here.

(2) register the custom httphandler in the Web Application

Add registration information to the Web. config network application configuration file.

<Httphandlers>
<Add verb = "Path =" *. jpg "type =" Name of the Custom Handler class, network application name "/>
</Httphandlers>

4. Let's see how the processrequest (system. Web. httpcontext context) method processes the HTTP request for the image file.
Public void processrequest (system. Web. httpcontext context)
{
If (context. request. urlreferrer. host = "www.frontfree.net") // checks whether the image is a local reference. If yes, it returns the correct image to the client, the judgment here is the reference page information recorded in the HTTP request described above.
{
Context. response. expires = 0; // set the file expiration time in the client buffer to 0, that is, it expires immediately.
Context. response. Clear (); // clear the output cache opened by the server for this session
Context. response. contenttype = getcontenttype (context. Request. physicalpath); // obtain the file type
Context. response. writefile (context. Request. physicalpath); // write the request file to the server end's output cache for this session
Context. response. End (); // transmits the information in the output cache opened by the server for this session to the client.
}
Else // if it is not a local reference, it is a leeching reference and an error image is returned to the client.
{
Context. response. expires = 0; // set the file expiration time in the client buffer to 0, that is, it expires immediately.
Context. response. Clear (); // clear the output cache opened by the server for this session
Context. response. contenttype = getcontenttype ("error.jpg"); // obtain the file type
Context. response. writefile ("error.jpg"); // write the image file with special report errors to the output cache opened by the server for this session
Context. response. End (); // transmits the information in the output cache opened by the server for this session to the client.
}
}

 

5. When everything is done, we need to let IIS know and process image files.

We also need to let IIS know about this. why is the aspx file processed? The default httphandler that can process aspx is actually because IIS automatically loadsC: \ windows \ Microsoft. NET \ framework \ v1.1.4322 \ aspnet_isapi.dllThis file is used to process the ASPX page and is a part of the. NET Framework. We need IIS to load this DLL file for it when it receives a JPG file request.

First, add a processing tool. You can configure the JPG file by referring to the configuration of the aspx file. We also load the aspnet_isapi.dll file for the JPG file, because the processing of JPG files is also based on ASP.. net. Specific configuration, such:

 

OK. Now you have some knowledge about this method. In fact, there are other custom httphandler applications. We will not go into details here. The following work requires more practices from readers, learn more and gain more experience to better apply the design and development requirements.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.