ASP.NET筆記之 Request 、Response 與Server的使用

下面做一個實例,通過Request的一些方法來判斷瀏覽圖片是不是在內(nèi)部瀏覽,還是直接按網(wǎng)址瀏覽或者被外部使用
<%@ 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("圖片只允許在博客園內(nèi)部查看", new System.Drawing.Font("宋體", 15),
System.Drawing.Brushes.Red, 0, 0);
}
//轉(zhuǎn)化成流格式輸出
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,往緩存里存,不是直接給瀏覽器,等到存滿了或者處理完成才發(fā)送到瀏覽器
* Flush方法,立即發(fā)給瀏覽器

(2)反盜鏈等:不往下執(zhí)行了在aspx寫較好
context.Response.End();
(3)aspx 和ashx
像輸出文本、圖片、下載地址最后是寫在ashx里面,而html內(nèi)容則寫在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+"步開始執(zhí)行<br/>");
//采用Flush,立即發(fā)給客戶端,效果很明顯!
context.Response.Flush();
}
}
public bool IsReusable {
get {
return false;
}
}
}
3、server
(1)Server.Transfer和 Response.Redirect的區(qū)別
*transfer訪問只能是內(nèi)部網(wǎng)站,不能是外部的,而redirect可以
* transfer是網(wǎng)站內(nèi)部接管的,只執(zhí)行一次http請求,而Redirect則是轉(zhuǎn)幾次就執(zhí)行幾次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"){
//請求的信息都會傳入服務(wù)器
Server.Transfer("DSCF0738.JPG");
}
else if (string.IsNullOrEmpty(text))
{
Response.Write("空");
}
else {
//不會取到傳過來的參數(shù)
Response.Redirect("蘋果.jpg");
//除非自己給它穿過去
//Response.Redirect("aaa.aspx?id=1");
}
}
}
- Asp.net內(nèi)置對象之Request對象(概述及應(yīng)用)
- Asp.net中Request.Url的各個屬性對應(yīng)的意義介紹
- ASP.NET從客戶端中檢測到有潛在危險的request.form值的3種解決方法
- asp.net HttpWebRequest自動識別網(wǎng)頁編碼
- asp.net下Request.QueryString取不到值的解決方法
- asp.net中Request.QueryString與Request.Param的區(qū)別分析
- asp.net Request獲取url信息的各種方法比較
- Asp.net response對象與request對象使用介紹
- asp.net request.PathInfo實現(xiàn)的url重寫
- asp.net實現(xiàn)遍歷Request的信息操作示例
相關(guān)文章
ASP.NET開發(fā)中經(jīng)常用到10款工具軟件介紹
從事.NET開發(fā)也好幾年了,工作過程中積累一些軟件工具,分享給大家,排名不分先后,希望對大家有所幫助。2016-04-04
ASP.NET與ASP互通COOKIES的一點經(jīng)驗
ASP與ASP.NET互相整合時,其中文COOKIES信息無法被互通共享,當使用ASP.NET寫入中文COOKIES信息后,使用ASP進行讀取,讀出來的卻是亂碼,而非中文。2010-03-03
ASP.NET?Core使用EF創(chuàng)建關(guān)系模型
這篇文章介紹了ASP.NET?Core使用EF創(chuàng)建關(guān)系模型的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04
在應(yīng)用程序級別之外使用注冊為allowDefinition=''MachineToApplication''的節(jié)是錯誤的
在應(yīng)用程序級別之外使用注冊為 allowDefinition='MachineToApplication' 的節(jié)是錯誤的2009-03-03
.NET Framework常用ORM框架iBatis.Net操作數(shù)據(jù)庫的方法
iBatis.Net 是一個輕量級的 ORM 框架,它允許開發(fā)者通過直接編寫 SQL 查詢來操作數(shù)據(jù)庫,并將查詢結(jié)果映射到對象模型中,本文將通過實際的代碼示例,詳細介紹如何在 .NET 環(huán)境中使用 iBatis.Net 進行數(shù)據(jù)庫操作,感興趣的朋友一起看看吧2024-08-08

