After all, image leeching is a successful task. Many people do not want others to steal images easily. This function is available in many forums, probably because there are too many leeching behaviors.
The anti-leeching program is actually very simple and familiar with ASP. NET application life cycle, you can easily write one, use HttpModule to intercept the request in the BeginRequest event is OK, the rest of the work is to filter, then filter!
If you are not familiar with HttpModule, go to MSDN for details. The address is ms-help: // MS. VSCC. v80/MS. MSDN. v80/MS. visual Studio. v80.chs/dv_aspnetcon/html/f1d2910f-61d0-4541-8af8-c3c108ca351f.htm. no nonsense here
Copy codeThe Code is as follows:
Private void Application_BeginRequest (Object source, EventArgs e)
{
HttpApplication application = (HttpApplication) source;
HttpContext context = application. Context;
Bool isSafe = true; // whether the link is valid
String uri = context. Request. Url. AbsolutePath. ToLower ();
If (uri. LastIndexOf (".")> 0 & context. Request. UrlReferrer! = Null)
{
String exp = uri. Substring (uri. LastIndexOf (".");
// Check whether the file suffix is in the excluded file type list.
Bool isHas = ClassLibrary. RData. RString. StrIsIncUseSC (exp, config. ImgSafeType. Split ('| '));
If (isHas)
{
String domainOutter = context. Request. UrlReferrer. Authority. ToLower (); // contains the domain name and port
ArrayList arry = Common. Cache. GetDomainValid (); // obtain the list of valid domain name bindings defined by the system.
IsSafe = arry. Contains (domainOutter); // checks whether the Domain Name of the current request is in the valid list.
}
}
// The following is the output when it is invalid. If an image is replaced by default, the output is generated. If not, the output is in the format. Gif
If (! IsSafe)
{
Bitmap img = null;
Graphics g = null;
MemoryStream MS = null;
Try
{
String picPath = ClassLibrary. RPath. GetFullDirectory ("images/unlawful.gif ");
If (File. Exists (picPath ))
{
Img = new Bitmap (picPath, false );
}
Else
{
Img = new Bitmap (**,**);
G = Graphics. FromImage (img );
G. Clear (Color. White );
Font f = new Font (",, Arial", 9, FontStyle. Bold );
SolidBrush s = new SolidBrush (Color. Red );
G. DrawString (Resources. Message. LawlessLink, f, s, 1, 20 );
Img. Save (picPath, ImageFormat. Gif );
}
MS = new MemoryStream ();
Img. Save (MS, ImageFormat. Gif );
Context. Response. ClearContent ();
Context. Response. ContentType = "image/Gif ";
Context. Response. BinaryWrite (ms. ToArray ());
Context. Response. End ();
}
Catch
{}
Finally
{
If (g! = Null)
G. Dispose ();
Img. Dispose ();
}
}
}
Any advantage is harmful. The biggest drawback of this operation is that the system overhead is increased. Every request of the client must be filtered out, and the performance will naturally be compromised. I don't know which friend has a better solution or an optimization method to discuss it together.
Implement the file leeching function and resume:
First, add a Global File Global. asax.
In Application_BeginRequest, we can determine whether the UrlReferre in the Http packet header is from this site.
Copy codeThe Code is as follows:
If (HttpContext. Current. Request. UrlReferrer! = Null)
{
If (HttpContext. Current. Request. Url. AbsolutePath. EndsWith ("jpg", StringComparison. OrdinalIgnoreCase) & HttpContext. Current. Request. UrlReferrer. Host! = "Localhost ")
{
HttpContext. Current. Response. WriteFile (HttpContext. Current. Server. MapPath ("~ /Jzdl.jpg "));
HttpContext. Current. Response. End ();
}
}