1.1 Response對象和Request對象
Request對象是ASP.NET中HTTP通信對象過程中的重要對象。其中Request對象發出HTTP請求,Response對象響應HTTP請求,它們是組成ASP.NET中的HTTP通信過程,不可缺少的一部分。
獲取Response對象的信息並輸出
在ASP.NET中的HTTP通信過程中,HttpResponse對象響應Http請求。其中,為了編程方便,HttpResponse對象通過Response對象進行公開。實際上,Response對象是HttpResponse類的一個實例。HttpResponse對象封裝了ASP.NET中的Http請求中的響應信息,並提供對當前頁的輸出流操作方法。HttpResponse對象能夠向網頁輸出信息,如文本,圖片等,並且還為輸出的信息提供緩沖區。
HttpResponse對象不但可以在網頁上輸出信息,而且可以使用End()方法或中止網頁的輸出。其包含了許多常用的方法,如 Write(),WriteFile(),Flush(),End(),Close()等
用反射機制遍歷HttpResposne對象的所有成員(包括方法和屬性)的信息,如果成員是方法,則在方法的名稱之后添加“()”代碼如下:
private void PrintResposneInfo() { // 獲取Response實例 // HttpResponse response = HttpApplication; Type t = Response.GetType(); foreach (MemberInfo mi in t.GetMembers()) { // 輸出成員信息 if(mi.GetType().BaseType.Name == "MethodInfo") { Response.Write("MemberInfo"+mi.Name+"():</br>"); } else { Response.Write("MemberInfo"+mi.Name+"()"); } Response.Write(" DeclaringType:"+mi.DeclaringType.ToString()); Response.Write(" MemberType:"+mi.MemberType.ToString()); Response.Write(" MetadataToken:"+mi.MetadataToken.ToString()); Response.Write(" ReflectedType:"+mi.ReflectedType.ToString()); } }
1.2. Http請求上下文
Http請求上下文(HttpContext對象)是ASP.NET中的重要對象,它包括了ASP.NET在HTTP請求中的大部分對象的信息,如Request,Response,Server,Modules 等對象。HttpContext對象 在ASP.NET應用程序的Http請求的過程各個步驟中保持各個對象之間的聯系。HttpContext對象封裝了ASP.NET中單個Http請求的所有可用的信息,並在請求處理機制時由HttpRunTime對象實例化。HttpContext對象在ASP.NET的Http請求的過程中與各個對象之間的關系:
圖1-1 HttpContext對象在ASP.NET的Http請求中與各個對象之間關系
1.3 應用程序的Application實例
在Asp.net 應用程序中,HttpApplication 實例是唯一的,表示Http請求過程中的整個應用,當應用程序開始運行時,HttpApplication 實例由Asp.Net基本運行庫創建,開發過程中不需要顯示創建HttpApplication類的實例。如果需要HttpApplication實例,直接獲取即可。
// 獲取應用程序HttpApplication 實例 HttpApplication thisHttpApp = ApplicationInstance;
ASP.NET應用程序運行之后,首先創建HttpRuntime對象,然后由HttpApplicationFactory對象創建或選擇 HttpApplication對象,然后由HttpApplication處理ASP.NET應用程序請求資源。
創建一個類(MyHttpModule),該類介紹了使用Http上下文對象(HttpConext)的方法。MyHttpModule類繼承於接口IHttpModule(處理Http請求過程中的模塊信息的一個接口),首先定義函數Init(HttpApplication httpApp) ,並在該函數中初始化應用程序的HttpApplication實例的AcquireRequestState事件和PostAcquireRequestState事件。其中AcquireRequestState事件和PostAcquireRequestState事件分別使用HttpContext對象和Resposne對象的Write()方法顯示事件名稱:
class MyHttpModule:IHttpModule { public void Dispose() { throw new NotImplementedException(); } public void Init(HttpApplication context) { context.AcquireRequestState += new EventHandler(context_AcquireRequestState); context.PostAcquireRequestState += new EventHandler(context_PostAcquireRequestState); } void context_PostAcquireRequestState(object sender, EventArgs e) { HttpApplication httpApp = (HttpApplication)sender; HttpContext context = HttpContext.Current; context.Response.Write("context_PostAcquireRequestState"); } void context_AcquireRequestState(object sender, EventArgs e) { HttpApplication httpApp = (HttpApplication)sender; HttpContext context = HttpContext.Current; context.Response.Write("context_AcquireRequestState"); } }
新建一個頁面,頁面加載時首先獲取應用程序HttpApplication實例,然后創建MyHttpModule的實例http並調用函數Init(HttpApplication httpApp)進行初始化,隨后觸發HttpApplication實例的AcquireRequestState和PostAcquireState事件,最后顯示HttpApplication實例包含ASP.NET應用程序的重要對象的信息。
protected void Page_Load(object sender, EventArgs e) { HttpApplication thisHttpApp = new HttpApplication(); MyHttpModule http = new MyHttpModule(); http.Init(thisHttpApp); // 調用 AcquireRequestState http.context_AcquireRequestState(thisHttpApp,e); // 調用 PostAcquireRequestState http.context_PostAcquireRequestState(thisHttpApp,e); Response.Write(thisHttpApp.Application.ToString()); Response.Write(thisHttpApp.Context.ToString()); Response.Write(thisHttpApp.Modules.ToString()); Response.Write(thisHttpApp.Request.ToString()); Response.Write(thisHttpApp.Response.ToString()); Response.Write(thisHttpApp.Server.ToString()); Response.Write(thisHttpApp.Session.ToString()); }