Image anti-leech

Source: Internet
Author: User

 

Using System;

Using System. Web;

 

 

/// <Summary>

/// Abstract description of ImageProtect

/// </Summary>

Public class ImageProtect: IHttpHandler

{

Public ImageProtect ()

{

//

// TODO: add the constructor logic here

//

}

Public void ProcessRequest (HttpContext context)

{

// Determine whether the image is a local reference. If yes, the correct image is returned to the client,

// Assume that the website address is http: // localhost: 6999. You can modify the address as needed during demonstration.

If (context. Request. UrlReferrer. Host = "localhost" & context. Request. UrlReferrer. Port = 6999)

{

// Set the file expiration time in the client buffer to 0, that is, it expires immediately.

Context. Response. Expires = 0;

// Clear the output cache opened by the server for this session

Context. Response. Clear ();

// Obtain the file type

Context. Response. ContentType = "image/jpeg ";

// Write the request file to the server's output cache for this session

Context. Response. WriteFile (context. Request. PhysicalPath );

// Transfer the information in the output cache opened by the server for this session to the client

Context. Response. End ();

}

Else // if it is not a local reference, it is a leeching reference and an error image is returned to the client.

{

// Set the file expiration time in the client buffer to 0, that is, it expires immediately.

Context. Response. Expires = 0;

// Clear the output cache opened by the server for this session

Context. Response. Clear ();

// Obtain the file type

Context. Response. ContentType = "image/jpeg ";

// Write the image file with special report errors to the output cache opened for this session on the server side

Context. Response. WriteFile ("~ /Images/error.jpg ");

// Transfer the information in the output cache opened by the server for this session to the client

Context. Response. End ();

}

 

}

Public bool IsReusable

{

Get

{

Return false;

}

}

}

 

/*

* In the configuration file

* <HttpHandlers>

* <Add verb = "*" path = "*. jpg" type = "ImageProtect"/>

</HttpHandlers>

*/

From happy pig's column

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.