1 what is image leeching
let's first analyze the general browsing phenomenon. The most important thing 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 one is HTTP the request is sent back with the text of this page, then explain the text in the browser of the client and find that there are still images in it. Then, the browser of the client will send another 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 have to be sent multiple times HTTP requests can be completely displayed. based on this mechanism, a problem occurs. is a leeching problem : is a website that does not contain the information in the page, such as the sample information, it can be connected to other websites. in this way, websites without any resources use resources of other websites to show visitors and increase their access traffic, which is not easily discovered by most viewers. Obviously, it is unfair for the website that has been used resources.
2Solution
So use it nowASP. NETInHttphandlerThis problem can be solved well because we only process dynamic web pages by default,ImageASP, aspxBut when an image file is requested,IISIt will directly extract resources and send them to the client. It seems a bit blind.
So we need to create our ownHttphandlerTo process image files. For exampleJPGFile.
3Then how do we build our ownHttphandlerAndWebApplicationProgramWhat about registration?
(1) Create your ownHttphandler
Create an inheritedSystem. Web. ihttphandlerInterface Class, inSystem. Web. ihttphandlerThe interface has only two members.
1 isreusableAttribute. The returned value indicates otherHTTPWhether the request can use the inheritedSystem. Web. ihttphanderThe instance of the interface class.
2Processrequest (system. Web. httpcontext context) Method, except for the specialHTTPRequest.
Parameters System. Web. httpcontextClass is loaded with all the information required by the HTTP protocol in an HTTP request. WhereSystem. Web. httpcontextClass contains the property request so that the value of the HTTP request information sent from the client can be easily read; the property response encapsulates the information and operations to be returned to the client. Of course, there are many common attributes and methods.NoFor details, we only use these two attributes here.
(2)WebRegister the customHttphandler
In Web. configAdd registration information to the network application configuration file
< Httphandlers > < Add Verb =" Path = "*. Jpg" Type = "Name of the Custom Handler class, network application name" /> </ Httphandlers > |
4So let's lookProcessrequest (system. Web. httpcontext context)How to request image filesHTTPRequest for processing.
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 need to makeIIS knows why the aspx file is processed, and 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 method. For more information, seeThe aspx file can be configured to process JPG files. We also load the aspnet_isapi.dll file for JPG files, because the processing of JPG files is also based on the ASP. NET mechanism. Specific configuration, such:
OK. Now you have some knowledge about this method. In fact, you can customize it.HttphandlerThere are other applications that we will not go into details here. The following work requires readers to practice more and learn more from them to gain more experience, better application design and development needs.