1. Request.AppRelativeCurrentExecutionFilePath,获取当前执行请求相对于应用根目录的虚拟路径,以~开头,比如"~/default.ashx"
  2. Request.PhysicalApplicationPath,获取当前应用的物理路径,比如:d:\VS2010\website\
  3. Request.PhysicalPath,获取当前请求的物理路径,即包括文件名,比如:d:\vs2010\website\default.aspx
  4. Request.RawUrl,获取原始请求的URL、Request.Url获得请求的URL,区别涉及到URL重写的问题
  5. Request.UrlReferrer:网页来源,可以根据这个判断访问的网页是来源于哪里,可以防搜索,防下载盗链、防图片盗链,也可以伪造,比如我们可以设置一下一个图片只能内部使用,其它的连接是不能用的。全局防盗链用Globals.asax.
  6. Request.UserHostAddress,获得访问者的IP地址
  7. Request.UserLanguages:获得访问者浏览器支持的语言,可以通过这个实现不同语言的人显示不同语言的页面。
  8. Request.Cookies,获取浏览器发过来的浏览器端的Cookie,从它里面读取Cookie的值,比如Context.Request.Cookies["sessionid"],使用Request.Cookies的时候一般只是读取,将Cookie写回浏览器要用Response.SetCookies[].
  9. Request.MapPath(virtualPath),将虚拟路径转为磁盘上的物理路径。

分别执行以上的函数:如下代码:

 protected void Button1_Click(object sender, EventArgs e)
{
string br = "<br />";
Response.Write("AppRelativeCurrentExecutionFilePath: "+Request.AppRelativeCurrentExecutionFilePath+br);
Response.Write("PhysicalApplicationPath: "+Request.PhysicalApplicationPath+br);
Response.Write("PhysicalPath: "+Request.PhysicalPath+br );
Response.Write("RawUrl: "+Request.RawUrl+br);
Response.Write("UrlReferrer: "+Request.UrlReferrer.Host + br);
Response.Write("UserHostAddress: "+Request.UserHostAddress + br);
Response.Write("UserHostName: "+Request.UserHostName + br);
Response.Write("UserLanguages: "+Request.UserLanguages[0] + br);
Response.Write("MapPath: "+Request.MapPath("~/Default.aspx")); }

则它们的显示结果为:

以下为防盗链的,主要用到UrlReferrer对象,在httpwatch中可以看到Request提示报文时有这么个对象。

我们在客户端浏览一张图片,检测一下这个对象的值,如果它的值为null,说明这个request不是从客户端的指定页面过来的,是直接运行了处理程序,就为null.如果是Request.UrlReferrer.Host为localhost则可以正常浏览,如果不是localhost则表明提交的网页来自其它网址,拒绝它访问。

服务端,我们用一般程序处理:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging; namespace 防盗链
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/JPEG";
string picpath = context.Server.MapPath("~/imgs/2.jpg"); using (Bitmap bmp = new Bitmap(picpath))
{
using (Graphics g = Graphics.FromImage(bmp))
{
if (context.Request.UrlReferrer == null)//如果直接浏览,则UrlReferrer为null
{
g.Clear(Color.White);
g.DrawString("禁止直接浏览图片,请在页面中查看图片", new Font("宋体", 30), Brushes.Red, 0, 0);
}
else if (context.Request.UrlReferrer.Host != "localhost")
{
g.Clear(Color.White);
g.DrawString("本图片仅限本机用", new Font("宋体", 30), Brushes.Red, 0, 0);
}
}
bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
} public bool IsReusable
{
get
{
return false;
}
}
}
}

客户端我们就用个超连接即可:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="Handler1.ashx">图片</a>
</div>
</form>
</body>
</html>

当我们直接在浏览器中访问一般处理程序时,即http://localhost:6023/Handler1.ashx,则Request.UrlReferrer为null,如果把localhost改成127.0.0.1,则它拒绝访问。

最新文章

  1. 进击的Python【第二十章】
  2. [JS] HTML QQ分享界面js代码
  3. 迅为iTOP-4412核心板调整电压
  4. Caffe学习系列(18): 绘制网络模型
  5. mongodb初步使用
  6. win7设置防火墙允许Ping与telnet
  7. Android Learning:多线程与异步消息处理机制
  8. Python报错:SyntaxError: Non-ASCII character &#39;\xe5&#39; in file
  9. python cmd命令调用
  10. swift + xcode 新手上路
  11. .net 实战 根据configuration选项生成不同的config文件
  12. python 数据聚合与分组
  13. futex-based pthread_cond
  14. spring项目获取ServletContext
  15. Azure 标准与高级托管磁盘存储的相互转换
  16. LeetCode题解之Convert BST to Greater Tree
  17. JAVA反射机制_获取字节码文件对象
  18. ELK安装部署
  19. [转]TensorFlow---岂止深度学习
  20. CentOS7.2配置vsftpd

热门文章

  1. win7 linux双系统删除linux
  2. c++继承总结
  3. 中小型研发团队架构实践:Redis快速入门及应用
  4. C#定时任务的偷懒实现
  5. Android_JarZip压缩和解压文件
  6. NFS介绍
  7. hdu1010Tempter of the Bone(dfs+奇偶剪枝)
  8. MySQL索引长度限制问题
  9. 两DD-WRT组建WDS设置
  10. 点滴积累【JS】---JS小功能(checkbox实现全选和全取消)