当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET笔记之 Request 、Response 与Server的使用

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

2017年12月12日  | 移动技术网IT编程  | 我要评论
1、request      下面做一个实例,通过request的一些方法来判断浏览图片是不是在内部浏览,还是直接按网址浏览或者被外部使
1、request

    

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

复制代码 代码如下:

<%@ webhandler language="c#" class="image_test" %>

 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 (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

   <img src="image_test.ashx" />啦啦啦啦

 2、response

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

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

   (3)aspx 和ashx

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

    (4)重定向  redirect

          

       flush实例:

复制代码 代码如下:

 <%@ webhandler language="c#" class="response" %>

using system;
using system.web;

public class response : ihttphandler {

    public void processrequest (httpcontext context) {

        //如果是plain则<br/>无效果
        context.response.contenttype = "text/html";
        //耗时操作
        for (int i = 0; i < 20; i++)
        {
            system.threading.thread.sleep(500);
            context.response.write("第"+i+"步开始执行<br/>");
            //采用flush,立即发给客户端,效果很明显!
            context.response.flush();
        }
    }

    public bool isreusable {
        get {
            return false;
        }
    }

}
 


3、server

  (1)server.transfer和 response.redirect的区别

         *transfer访问只能是内部网站,不能是外部的,而redirect可以

        * transfer是网站内部接管的,只执行一次http请求,而redirect则是转几次就执行几次http请求并在地址栏中显示

         * transfer会直接把各种信息传过去而redirect不会
         * 不能重新定向到ashx transfo

           

              

实例:

复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;

public partial class vivideo_test_server_server : system.web.ui.page
{
    protected void page_load(object sender, eventargs e)
    {
        string text=request["id"];
        if (text == "1") {
            response.write("一");
        }
        else if(text=="2"){
            //请求的信息都会传入服务器
            server.transfer("dscf0738.jpg");
        }
        else if (string.isnullorempty(text))
        {
            response.write("空");
        }
        else {
            //不会取到传过来的参数
            response.redirect("苹果.jpg");
            //除非自己给它穿过去
            //response.redirect("aaa.aspx?id=1");

        }
    }
}

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

相关文章:

验证码:
移动技术网