<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="">xxd</a>
<h1>图片</h1>
<image src="Image.ashx"></image>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
/* Request对像
* 1 request.AppRelativeCurrentExecuteionFilePath,获取当前执行请求相对于应用根目录的虚拟路径,以~开头,比如"~/Handler.ashx"
* 2 Request.PhysicalApplicationPath,获取当前应用的物理路径,比如D:\我的文档\xxdxxd\
* 3 Request.PhysicalPath,获取当前请求的物理路径, 比如D:\我的文档\a.ashx
* 4 Request.RawUrl获得原始请求URL,Request Url获得请求的URL,区别涉及到URL重写的问题
* 5 Request.UrlReferrer网页的来源,可以根据这个判断从百度搜的哪个关键词,防下载盗链,防图片盗链,可以伪造.
* "本图片仅供内部交流使用",在DZ中测试,全局防盗链用Globals.asax
* 6 Request.UserHostAddress获得访问者的ip地址
* 7 Request.UserLanguages获得访问者浏览器支持的语言,可以通过这个实现对不同语言的人显示不同语言的页面
* 8 request.Cookies获取浏览器发过来的浏览器端的cookie,从它里面读取cookie值,比如
* context.Request.Cookies["mySessionid"],使用Request.Cookies的时候一般只是读取,将Cookie写回浏览器要用Response.Cookies
*
*
* Response对像
* 响应的缓冲输出:为了提高服务器的性能,asp.net向浏览器write的进修默认并不会每write一次都立即输出到浏览器,而是会缓存数据,到合适的时机或者响应结束才传动轴将缓存区中的数据一起发送到浏览器
*
* Response对像的主要成员:
* 1 Response.Buffer, Response.BufferOutput: 通过Reflector反编译,发现两个属性是一样的,
* Buffer内部就是调用的BufferOutput,这个属性用来控制是否采用响应缓存,默认是true
*
* 2 Response.Flush()将缓存区中的数据发送给浏览器,这在需要将Write出来的内容立即输出到浏览器的场合非常适用,案例,大批量的数据导入,显示正在导入的第*条数据,用Thread.Sleep模拟耗时
*
* 3 Response.Clear()清空缓存区中的灵敏据,这样在缓存区中的没有发送到浏览器端的数据被清空,不会被发送到浏览器
*
* 4 Response.ContentEncoding输了流的编码
*
* 5 Response.ContentType输出流的内容类型, 比如是html(text/html)还是普通文本(text/plain)还是JPEG图片(image/JPEG)
*
*
* Response对像2
* 6 Response.Cookies返回给浏览器的Cookie的集合,可以通过它设置Cookie
* 7 response.OutputStream输出流,在输出图片,Excel文件等非文本内容的时候要使用它
* 8 Response.End() 终止响应,将之前缓存中的数据发给浏览器,End()之后的代码不会被继承执行,在终止一些非法请求的时候,比如盗链等可以用End()立即终止请求
* 9 Response.Redirector(url)重定向浏览器到新的网址,即可以重定向到站外网址也可以重定向站内网址,
* response.Redirector("www.sina.com.cn")
* Response.Redirector("a.htm"),Redirect是向浏览器发回302重定向,是通过浏览器"请重新访问url这个网址"
* 这个过程经历了服务器通知浏览器"请重新访问url"这个网址和"浏览器接到命令方步问新网址的过"
* 使用httpWatch查看整个响应过程序的http报文,用Redirect因为是浏览器自己去重新访问新网址的,所以在地址栏中是可以看到网址的变化的
* 10 Response.SetCookie(HttpCookie cookie),向输出流中更新写到浏览器中的Cookie
* 如果Cookie存在就更新不存在就增加,是对Response.Cookies的简化调用
*
* 11 Response.Write()向浏览器输出内容
* 12 Response.WriteFile(filename)向浏览器输出文件,比如
* Response.WritelFill("c:/boout.ini");
*
*/
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//取得当前文件位于应用的相对虚拟路径
Response.Write(Request.AppRelativeCurrentExecutionFilePath);
Response.Write("<BR>");
//当前的物理目录
Response.Write(Request.PhysicalApplicationPath);
Response.Write("<BR>");
//当前的物理文件目录,包括文件名
Response.Write(Request.PhysicalPath);
Response.Write("<BR>");
//获得原始请求的url
Response.Write(Request.RawUrl);
Response.Write("<BR>");
Response.Write(Request.UrlReferrer);
Response.Write("<BR>");
Response.Write(Request.UserHostAddress);
Response.Write("<BR>");
Response.Write(Request.UserLanguages);
Response.Write("<BR>");
Response.Write(Request.MapPath("Default.aspx"));
Response.Write("<BR>");
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="end.aspx.cs" Inherits="end" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class end : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Response.Write("当前网站禁止访问");
//Response.End();
//Response.Write("您能看到我吗?");
/*string q = Request["q"];
if (q == "1")
{
Response.Write("您输入的值是1");
}
else if (string.IsNullOrEmpty(q))
{
Response.Write("请输入你的问题!");
}
else
{
Response.Redirect("1.jpg");
}*/
//Response.ContentType = "image/JPEG";
//string fullPath = Server.MapPath("3.jpg");
//Response.WriteFile(fullPath);
Response.ContentType = "text/html";
Response.WriteFile("c:/a.txt");
}
}
<%@ WebHandler Language="C#" Class="Image" %>
using System;
using System.Web;
public class Image : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "image/JPEG";
//context.Response.Write("Hello World");
string fullpath = HttpContext.Current.Server.MapPath("3.jpg");
//using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(200, 200))
using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(fullpath))
{
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
{
//如果直接访问图片UrlReferrer就是null,如果是嵌入到别的网页中的请求,UrlReferrer就是页页的地址
if (context.Request.UrlReferrer == null)
{
//g.Clear(System.Drawing.Color.White);
g.DrawString("禁止直接浏览图片,请在页面中查看图片",
new System.Drawing.Font("宋体",30),System.Drawing.Brushes.Red, 0,0);
}
else if(context.Request.UrlReferrer.Host!="localhost"){
//g.Clear(System.Drawing.Color.White);
g.DrawString("本图片仅限内部交流使用",new System.Drawing.Font("宋体",30),System.Drawing.Brushes.Red,0,0);
}
g.DrawString("你的IP是:" + context.Request.UserHostAddress, new System.Drawing.Font("宋体", 30), System.Drawing.Brushes.Red, 0, 100);
if (context.Request.UserHostAddress == "127.0.0.1")
{
g.Clear(System.Drawing.Color.Yellow);
g.DrawString("您的IP已经不能访问", new System.Drawing.Font("宋体", 22), System.Drawing.Brushes.Red, 0, 200);
}
}
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
public bool IsReusable {
get {
return false;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Image.aspx.cs" Inherits="Image" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Image : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "image/JPEG";
//context.Response.Write("Hello World");
string fullpath = HttpContext.Current.Server.MapPath("3.jpg");
//using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(200, 200))
using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(fullpath))
{
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
{
//如果直接访问图片UrlReferrer就是null,如果是嵌入到别的网页中的请求,UrlReferrer就是页页的地址
if (Request.UrlReferrer == null)
{
//g.Clear(System.Drawing.Color.White);
g.DrawString("禁止直接浏览图片,请在页面中查看图片",
new System.Drawing.Font("宋体", 30), System.Drawing.Brushes.Red, 0, 0);
}
else if (Request.UrlReferrer.Host != "localhost")
{
//g.Clear(System.Drawing.Color.White);
g.DrawString("本图片仅限内部交流使用", new System.Drawing.Font("宋体", 30), System.Drawing.Brushes.Red, 0, 0);
}
g.DrawString("你的IP是:" + Request.UserHostAddress, new System.Drawing.Font("宋体", 30), System.Drawing.Brushes.Red, 0, 100);
if (Request.UserHostAddress == "127.0.0.1")
{
g.Clear(System.Drawing.Color.Yellow);
g.DrawString("您的IP已经不能访问", new System.Drawing.Font("宋体", 22), System.Drawing.Brushes.Red, 0, 200);
}
}
bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
<%@ WebHandler Language="C#" Class="steup" %>
using System;
using System.Web;
public class steup : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
//context.Response.Write("Hello World");
for (int i = 0; i < 20; i++)
{
System.Threading.Thread.Sleep(500);
context.Response.Write("第"+i+"步执行完成!");
if (i == 10)
{
context.Response.Clear(); //清空旧的浏览器内容
}
context.Response.Flush(); //立即发送浏览器,不用缓存
}
}
public bool IsReusable {
get {
return false;
}
}
}