.htm格式网页中的图片如何防止盗链?现在被一些网站用<img src=http://我站域名/images/xxx.jpg>盗链了,有什么办法防止吗?网站用的是虚拟主机自己不能设置IIS.
解决方案 »
- 急啊! 怎么在后台获取动态生成的图片上传控件的文本!
- Gridview下点ImageButton没有反应?
- 什么是remoting,在什么情况下用
- 如何取得模板里控件的ID或者取得其值
- gridvie中帮定checkbox的问题
- 为什么老是提示:BC30002: 未定义类型“oledbconnection”。
- 我的dataTable.Rows.Count 为何返回值是0x2,我到数据库里看过应该是2的
- 如何在Word文档中的特定位置插入图片!
- 两个问题??
- 奇怪为什么不能显示我选中的值?
- 关于NbearLite
- 网站打开时出现其他网站的链接,但在IE8中可以正常访问,以下版本就不行,请大高手帮忙诊断!莫非是传说中的植入病毒
首先,我们需要对图片做如下引用:
<img src="/images/getimg.asp?FName=pic.jpg">
对getimg.asp我们做如下处理:
<%
Option Explicit
dim Server_Link,FilePath
Server_Link=request.ServerVariables("HTTP_REFERRER")
Server_Link=mid(Server_Link,InStr(Server_Link,".")+1)
Server_Link=left(Server_Link,InStr(Server_Link,"/")-1)
If Server_Link="code-123.com" then
FilePath="图片文件夹地址" + Request.QueryString("FName")
Else
FilePath="/images/非法链接.jpg"
End If
Response.Redirect(FilePath)
%>
可实现防盗链效果。
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Globalization;
using System.IO;
namespace HttpHandler
{
public class ImageHandler:IHttpHandler
{
#region IHttpHandler 成员
public void ProcessRequest(HttpContext context)
{
HttpRequest req = context.Request;
string IMG_path = req.PhysicalPath;
string contentType;
if (req.UrlReferrer != null && req.UrlReferrer.Host.Length > 0)
{
if (CultureInfo.InvariantCulture.CompareInfo.Compare(req.Url.Host, req.UrlReferrer.Host, CompareOptions.IgnoreCase) != 0)
{
IMG_path = context.Server.MapPath("~/images/error.gif");
}
contentType = GetContentType(IMG_path);
if (File.Exists(IMG_path))
{
context.Response.StatusCode = 200;
context.Response.ContentType = contentType;
context.Response.WriteFile(IMG_path);
}
else
{
context.Response.StatusCode = 404;
context.Response.Status = "无法找到你请求的文件";
}
}
}
public bool IsReusable
{
get { return true; }
}
#endregion
private string GetContentType(string path)
{
string extension = Path.GetExtension(path);
string type;
switch (extension)
{
case ".gif":
type = "image/gif";
break;
case ".jpg":
type = "image/Jpeg";
break;
case ".png":
type = "image/png";
break;
default:
type = "";
break;
}
return type;
}
}
}
web.config 文件里配置 <add verb="*" path="*.jpg" type="HttpHandler.ImageHandler, HttpHandler"/>
<add verb="*" path="*.gif" type="HttpHandler.ImageHandler, HttpHandler"/>
<add verb="*" path="*.png" type="HttpHandler.ImageHandler, HttpHandler"/>
不管你是html还是aspx页 都可以由picture.aspx 接管jpg picture.aspx.cs可以对来访地址进行判断 空 or!我站域名 输出error.jpg