2 Request对象的一些属性等

  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,则它拒绝访问。

——

原文地址:https://www.cnblogs.com/yagzh2000/p/3128042.html