asp.net学习——Request对象

  • Request.AppRelativeCurrentExecutionFilePath,获取当前执行请求相对于应用根目录的虚拟路径,以~开头,比如“~/Default.aspx”
  • Request.PhysicalApplicationPath,获取当前应用的物理路径。
  • Request.PhysicalPath,获取当前请求的物理路径。
  • Request.RawUrl获得原始请求URL,Request.Url获得请求的URL,区别涉及到URL重写的问题。
  • Request.UrlReferrer网页的来源,可以根据这个判断从百度搜到哪个关键字、防下载盗链、防图片盗链、可以伪造。
  • Request.UserHostAddress获得访问者的IP地址 (可以用来屏蔽对方的IP地址)
  • Request.UserLanguages获得访问者浏览器支持的语言,可以通过这个实现对不同语言的人显示不同语言的页面。
  • Request.Cookies获得浏览器发过来的浏览器端的Cookie,从它里面读取Cookie值,使用Request.Cookies的时候一般只是读取,Response.Coois将Cookie写回浏览器。
  • Request.MapPath(virtuPath)将虚拟路径转换为磁盘上的物理路径。

放盗链例子:

 

//html

<head>
    <title>防盗链图片</title>
</head>
<body>
<img src="防盗链图片生成.ashx" alt="" />
</body>

 

 

//防盗链图片生成.ashx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;

namespace asp.net入门2
{
    /// <summary>
    /// 防盗链图片生成 的摘要说明
    /// </summary>
    public class 防盗链图片生成 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/JPEG";
            string fullpath = HttpContext.Current.Server.MapPath("~/image/资料.jpg");
            using (Bitmap bmp = new Bitmap(fullpath))
            {
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    if (context.Request.UrlReferrer == null)
                    {
                        //当单独查看图片的时候,context.Request.UrlReferrer为空。
                        //无法单独访问图片
                        g.Clear(Color.White);
                        g.DrawString("禁止直接访问图片", new Font("宋体", 10), Brushes.Red, 3, 2);
                    }
                    else if (context.Request.UrlReferrer.Host != "localhost")
                    {
                        //这里的"localhost"可以改成任意的网址,当发现不是这个网址的时候,显示禁止
                        //防别的网址链接此图片
                        g.Clear(Color.White);
                        g.DrawString("此图片只能在本论坛查看", new Font("宋体", 10), Brushes.Red, 3, 2);
                    }
                }
                bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

asp.net学习鈥斺擱equest对象

从这里可以看出context.Request.UrlReferrer.Host

asp.net学习鈥斺擱equest对象

正常的

asp.net学习鈥斺擱equest对象

asp.net学习鈥斺擱equest对象

posted on 2013-03-12 15:32  AI_JJ  阅读(167)  评论(0)    收藏  举报

导航