• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

ASP.NET笔记之 Request 、Response 与Server的使用

asp 搞代码 4年前 (2022-01-03) 30次浏览 已收录 0个评论

本篇文章小编为大家介绍,ASP.NET笔记之 Request 、Response 与Server的使用。需要的朋友参考下

1、Request

    

下面做一个实例,通过Request的一些方法来判断浏览图片是不是在内部浏览,还是直接按网址浏览或者被外部使用

代码如下:

 using System;
 using System.Web;

 public class image_Test : IHttpHandler {

     public void ProcessRequest(HttpContext context)
     {
         context.Response.ContentType = “image/JPEG”;
         //如果直接访问URLreferrer 就是null ,如果嵌入到页面中请求
         //URLreferrer就是页面的地址
         string picPath=HttpContext.Current.Server.MapPath(“DSCF0738.JPG”);
         using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(picPath)) {
             using (System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(bitmap))
             {
                 //不过还是太脆弱,因为UrlReferrer还是由客户端提交
                 //迅雷破解毫无鸭梨
                 if (

来源gao!daima.com搞$代!码网

context.Request.UrlReferrer == null)
                 {
                     graphic.Clear(System.Drawing.Color.White);
                     graphic.DrawString(“禁止直接浏览图片”, new System.Drawing.Font(“宋体”, 15),
                         System.Drawing.Brushes.Red, 0, 0);

                 }
                 //http://127.0.0.1:32581/WebSite_zzl01/vivideo_test/request/Request.aspx

                 else if (context.Request.UrlReferrer.Host != “localhost”)
                 {
                     graphic.Clear(System.Drawing.Color.White);
                     graphic.DrawString(“图片只允许在博客园内部查看”, new System.Drawing.Font(“宋体”, 15),
                         System.Drawing.Brushes.Red, 0, 0);
                 }
                 //转化成流格式输出
                 bitmap.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);

             }
         }
     }

     public bool IsReusable {
         get {
             return false;
         }
     }

 }

html

   啦啦啦啦

 2、Response

   (1)返回 流,以流的形式返回给客户端
* 每次write,往缓存里存,不是直接给浏览器,等到存满了或者处理完成才发送到浏览器
* Flush方法,立即发给浏览器

   (2)反盗链等:不往下执行了在aspx写较好
       context.Response.End();

   (3)aspx 和ashx

        像输出文本、图片、下载地址最后是写在ashx里面,而html内容则写在aspx里面

    (4)重定向  Redirect

          

       Flush实例:

代码如下:
 

using System;
using System.Web;

public class response : IHttpHandler {

    public void ProcessRequest (HttpContext context) {

        //如果是plain则
无效果
        context.Response.ContentType = “text/html”;
        //耗时操作
        for (int i = 0; i <20; i++)
        {
            System.Threading.Thread.Sleep(500);
            context.Response.Write(“第”+i+”步开始执行
“);
            //采用Flush,立即发给客户端,效果很明显!
            context.Response.Flush();
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}
 

以上就是ASP.NET笔记之 Request 、Response 与Server的使用的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:ASP.NET笔记之 Request 、Response 与Server的使用

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址