ASP. NET image leeching

Source: Internet
Author: User

By default, IIS directly extracts resources for files such as http request images and sends them to the client. We need to create our own HttpHandler to process image files.

How to implement:

1. Create your own HttpHandler

Create a new class (MyHttpHandler) to inherit the System. Web. IHttpHandler interface and implement the corresponding method

  1. IsReusable attribute (a 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 (used to process custom special http requests to be processed ).

Parameters
An instance of the System. Web. HttpContext class is loaded with 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

In
Add registration information to Web. config

<HttpHandlers> <br/> <add verb = "*" path = "*. jpg" type = "MyHttpHandler, MyHttpHandler"/> <br/> </pttpHandlers>

3. Now let's lookProcessRequest (System. Web. HttpContext
Context)
The method is to process the http request for image files.

Public void ProcessRequest (HttpContext context) <br/>{< br/> // determines whether the request is a local reference. If yes, the system returns the correct image to the client, the judgment here is the reference page information recorded in the preceding http request <br/> if (context. request. urlReferrer. host = "localhost") {<br/> // set the file expiration time in the client buffer to 0, that is, it expires immediately. <Br/> context. response. expires = 0; <br/> // clear the output cache opened by the server for this session <br/> context. response. clear (); <br/> // obtain the file type <br/> context. response. contentType = getContentType (context. request. physicalPath); <br/> // write the request file to the output cache opened by the server for this session <br/> context. response. writeFile (context. request. physicalPath); <br/> // transmits the information in the output cache opened by the server for this session to the client. <br/> context. response. end (); <br/>}< br/> else // if it is not a local reference, it is a leeching reference, error image returned to the client <br/>{< br/> context. response. expires = 0; context. response. clear (); <br/> context. response. contentType = "image/jpg"; <br/> context. response. writeFile (context. request. physicalApplicationPath + "error.jpg"); <br/> context. response. end (); <br/>}< br/>}
5. finally, in the "configuration" of the default website in IIS, add the executable file mapped to the "Application ing" as the Framework installation path \ aspnet_isapi.dll "and the extension is ". jpg.

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.