ASP.NET笔记之 Request 、Response 与Server的使用
下面做一个实例,通过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");
}
}
}
- Asp.net内置对象之Request对象(概述及应用)
- Asp.net中Request.Url的各个属性对应的意义介绍
- ASP.NET从客户端中检测到有潜在危险的request.form值的3种解决方法
- asp.net HttpWebRequest自动识别网页编码
- asp.net下Request.QueryString取不到值的解决方法
- asp.net中Request.QueryString与Request.Param的区别分析
- asp.net Request获取url信息的各种方法比较
- Asp.net response对象与request对象使用介绍
- asp.net request.PathInfo实现的url重写
- asp.net实现遍历Request的信息操作示例
相关文章
解析GridView自带分页及与DropDownList结合使用
本文主要介绍了GridView自带的分页功能的实现方法。具有一定的参考价值,需要的朋友一起来看下吧2016-12-12asp.core 同时兼容JWT身份验证和Cookies 身份验证两种模式(示例详解)
这篇文章主要介绍了asp.core 同时兼容JWT身份验证和Cookies 身份验证两种模式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-02-02ASP.NET生成eurl.axd Http异常错误的处理方法
在IIS6中同时启用了ASP.NET 2.0 和 ASP.NET 4.0 后,网站程序可能会出现如下错误:“ System.Web.HttpException: Path ‘//eurl.axd/‘ was not found. ”2011-05-05关于dotnet 替换 ASP.NET Core 的底层通讯为命名管道的 IPC 库的问题
这篇文章主要介绍了dotnet 替换 ASP.NET Core 的底层通讯为命名管道的 IPC 库,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-02-02详解ASP.NET Core Web Api之JWT刷新Token
这篇文章主要介绍了详解ASP.NET Core Web Api之JWT刷新Token,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-11-11在ASP.NET使用JavaScript显示信息提示窗口实现原理及代码
在ASP.NET使用JavaScript显示信息窗口,感兴趣的朋友可以了解一下,本文将介绍详细的操作步骤,希望对你的JavaScript知识巩固有所帮助2013-01-01
最新评论