HttpResponse的Output與OutputStream、Filter關(guān)系與區(qū)別介紹
更新時間:2012年11月07日 15:13:27 作者:
在網(wǎng)上經(jīng)??匆娪羞@樣的代碼HttpResponse response = HttpContext.Current.Response;現(xiàn)在我也來說說這幾個東東是什么吧
在網(wǎng)上經(jīng)??匆娪羞@樣的代碼
HttpResponse response = HttpContext.Current.Response;
response.Filter = new PageFilter(response.Filter);
來攔截輸出流,自己也做個類似的東東,如asp.net中 js 合并 壓縮,現(xiàn)在我也來說說這幾個東東是什么吧,需要大家對asp.net的生命周期比較熟悉,如不熟悉的朋友建議先看看ASP.NET 請求處理流程 ASP.NET管線與應(yīng)用程序生命周期
首先我們來看看這3個屬性的源代碼吧:
public TextWriter Output
{
get
{
return this._writer;
}
set
{
this._writer = value;
}
}
public Stream OutputStream
{
get
{
if (!this.UsingHttpWriter)
{
throw new HttpException(SR.GetString("OutputStream_NotAvail"));
}
return this._httpWriter.OutputStream;
}
}
public Stream Filter
{
get
{
if (this.UsingHttpWriter)
{
return this._httpWriter.GetCurrentFilter();
}
return null;
}
set
{
if (!this.UsingHttpWriter)
{
throw new HttpException(SR.GetString("Filtering_not_allowed"));
}
this._httpWriter.InstallFilter(value);
IIS7WorkerRequest request = this._wr as IIS7WorkerRequest;
if (request != null)
{
request.ResponseFilterInstalled();
}
}
}
我們看到Filter和OutputStream都用到了一個屬性UsingHttpWriter,那這個屬性是怎么定義的了
private bool UsingHttpWriter
{
get
{
return ((this._httpWriter != null) && (this._writer == this._httpWriter));
}
}
從這個屬性我們可以知道_writer 、_httpWriter實際上是同一個東東,它們的類型是HttpWriter ,而HttpWriter 又繼承與TextWriter?,F(xiàn)在我們可以解釋Output就是_httpWriter,而OutputStream是_httpWriter的OutputStream屬性。類HttpWriter 主要代碼如下
public Stream OutputStream
{
get
{
return this._stream;
}
}
internal HttpWriter(HttpResponse response) : base(null)
{
this._response = response;
this._stream = new HttpResponseStream(this);
this._buffers = new ArrayList();
this._lastBuffer = null;
this._charBuffer = (char[]) s_Allocator.GetBuffer();
this._charBufferLength = this._charBuffer.Length;
this._charBufferFree = this._charBufferLength;
this.UpdateResponseBuffering();
}
internal HttpResponseStream(HttpWriter writer)
{
this._writer = writer;
}
HttpResponse 在Filter屬性設(shè)置調(diào)用了HttpWriter類的InstallFilter方法,而獲取調(diào)用了該類的GetCurrentFilter
internal void InstallFilter(Stream filter)
{
if (this._filterSink == null)
{
throw new HttpException(SR.GetString("Invalid_response_filter"));
}
this._installedFilter = filter;
}
internal Stream GetCurrentFilter()
{
if (this._installedFilter != null)
{
return this._installedFilter;
}
if (this._filterSink == null)
{
this._filterSink = new HttpResponseStreamFilterSink(this);
}
return this._filterSink;
}
由以上代碼我們可以得知HttpResponse的輸出流就是Filter屬性設(shè)置的流,即HttpResponse的Output和OutputStream屬性的輸出流都是來自Filter中的流。我們來看看_writer 、_httpWriter它們是在什么時候初始化的了?在HttpResonse中有一個方法
internal void InitResponseWriter()
{
if (this._httpWriter == null)
{
this._httpWriter = new HttpWriter(this);
this._writer = this._httpWriter;
}
}
該方法是由HttpRuntime的ProcessRequestInternal來調(diào)用
private void ProcessRequestInternal(HttpWorkerRequest wr)
{
HttpContext context;
try
{
context = new HttpContext(wr, false);
}
catch
{
wr.SendStatus(400, "Bad Request");
wr.SendKnownResponseHeader(12, "text/html; charset=utf-8");
byte[] bytes = Encoding.ASCII.GetBytes("<html><body>Bad Request</body></html>");
wr.SendResponseFromMemory(bytes, bytes.Length);
wr.FlushResponse(true);
wr.EndOfRequest();
return;
}
wr.SetEndOfSendNotification(this._asyncEndOfSendCallback, context);
Interlocked.Increment(ref this._activeRequestCount);
HostingEnvironment.IncrementBusyCount();
try
{
try
{
this.EnsureFirstRequestInit(context);
}
catch
{
if (!context.Request.IsDebuggingRequest)
{
throw;
}
}
context.Response.InitResponseWriter();
IHttpHandler applicationInstance = HttpApplicationFactory.GetApplicationInstance(context);
if (applicationInstance == null)
{
throw new HttpException(SR.GetString("Unable_create_app_object"));
}
if (EtwTrace.IsTraceEnabled(5, 1))
{
EtwTrace.Trace(EtwTraceType.ETW_TYPE_START_HANDLER, context.WorkerRequest, applicationInstance.GetType().FullName, "Start");
}
if (applicationInstance is IHttpAsyncHandler)
{
IHttpAsyncHandler handler2 = (IHttpAsyncHandler) applicationInstance;
context.AsyncAppHandler = handler2;
handler2.BeginProcessRequest(context, this._handlerCompletionCallback, context);
}
else
{
applicationInstance.ProcessRequest(context);
this.FinishRequest(context.WorkerRequest, context, null);
}
}
catch (Exception exception)
{
context.Response.InitResponseWriter();
this.FinishRequest(wr, context, exception);
}
}
HttpResponse response = HttpContext.Current.Response;
response.Filter = new PageFilter(response.Filter);
來攔截輸出流,自己也做個類似的東東,如asp.net中 js 合并 壓縮,現(xiàn)在我也來說說這幾個東東是什么吧,需要大家對asp.net的生命周期比較熟悉,如不熟悉的朋友建議先看看ASP.NET 請求處理流程 ASP.NET管線與應(yīng)用程序生命周期
首先我們來看看這3個屬性的源代碼吧:
復(fù)制代碼 代碼如下:
public TextWriter Output
{
get
{
return this._writer;
}
set
{
this._writer = value;
}
}
public Stream OutputStream
{
get
{
if (!this.UsingHttpWriter)
{
throw new HttpException(SR.GetString("OutputStream_NotAvail"));
}
return this._httpWriter.OutputStream;
}
}
復(fù)制代碼 代碼如下:
public Stream Filter
{
get
{
if (this.UsingHttpWriter)
{
return this._httpWriter.GetCurrentFilter();
}
return null;
}
set
{
if (!this.UsingHttpWriter)
{
throw new HttpException(SR.GetString("Filtering_not_allowed"));
}
this._httpWriter.InstallFilter(value);
IIS7WorkerRequest request = this._wr as IIS7WorkerRequest;
if (request != null)
{
request.ResponseFilterInstalled();
}
}
}
我們看到Filter和OutputStream都用到了一個屬性UsingHttpWriter,那這個屬性是怎么定義的了
復(fù)制代碼 代碼如下:
private bool UsingHttpWriter
{
get
{
return ((this._httpWriter != null) && (this._writer == this._httpWriter));
}
}
從這個屬性我們可以知道_writer 、_httpWriter實際上是同一個東東,它們的類型是HttpWriter ,而HttpWriter 又繼承與TextWriter?,F(xiàn)在我們可以解釋Output就是_httpWriter,而OutputStream是_httpWriter的OutputStream屬性。類HttpWriter 主要代碼如下
復(fù)制代碼 代碼如下:
public Stream OutputStream
{
get
{
return this._stream;
}
}
internal HttpWriter(HttpResponse response) : base(null)
{
this._response = response;
this._stream = new HttpResponseStream(this);
this._buffers = new ArrayList();
this._lastBuffer = null;
this._charBuffer = (char[]) s_Allocator.GetBuffer();
this._charBufferLength = this._charBuffer.Length;
this._charBufferFree = this._charBufferLength;
this.UpdateResponseBuffering();
}
internal HttpResponseStream(HttpWriter writer)
{
this._writer = writer;
}
HttpResponse 在Filter屬性設(shè)置調(diào)用了HttpWriter類的InstallFilter方法,而獲取調(diào)用了該類的GetCurrentFilter
復(fù)制代碼 代碼如下:
internal void InstallFilter(Stream filter)
{
if (this._filterSink == null)
{
throw new HttpException(SR.GetString("Invalid_response_filter"));
}
this._installedFilter = filter;
}
internal Stream GetCurrentFilter()
{
if (this._installedFilter != null)
{
return this._installedFilter;
}
if (this._filterSink == null)
{
this._filterSink = new HttpResponseStreamFilterSink(this);
}
return this._filterSink;
}
由以上代碼我們可以得知HttpResponse的輸出流就是Filter屬性設(shè)置的流,即HttpResponse的Output和OutputStream屬性的輸出流都是來自Filter中的流。我們來看看_writer 、_httpWriter它們是在什么時候初始化的了?在HttpResonse中有一個方法
復(fù)制代碼 代碼如下:
internal void InitResponseWriter()
{
if (this._httpWriter == null)
{
this._httpWriter = new HttpWriter(this);
this._writer = this._httpWriter;
}
}
該方法是由HttpRuntime的ProcessRequestInternal來調(diào)用
復(fù)制代碼 代碼如下:
private void ProcessRequestInternal(HttpWorkerRequest wr)
{
HttpContext context;
try
{
context = new HttpContext(wr, false);
}
catch
{
wr.SendStatus(400, "Bad Request");
wr.SendKnownResponseHeader(12, "text/html; charset=utf-8");
byte[] bytes = Encoding.ASCII.GetBytes("<html><body>Bad Request</body></html>");
wr.SendResponseFromMemory(bytes, bytes.Length);
wr.FlushResponse(true);
wr.EndOfRequest();
return;
}
wr.SetEndOfSendNotification(this._asyncEndOfSendCallback, context);
Interlocked.Increment(ref this._activeRequestCount);
HostingEnvironment.IncrementBusyCount();
try
{
try
{
this.EnsureFirstRequestInit(context);
}
catch
{
if (!context.Request.IsDebuggingRequest)
{
throw;
}
}
context.Response.InitResponseWriter();
IHttpHandler applicationInstance = HttpApplicationFactory.GetApplicationInstance(context);
if (applicationInstance == null)
{
throw new HttpException(SR.GetString("Unable_create_app_object"));
}
if (EtwTrace.IsTraceEnabled(5, 1))
{
EtwTrace.Trace(EtwTraceType.ETW_TYPE_START_HANDLER, context.WorkerRequest, applicationInstance.GetType().FullName, "Start");
}
if (applicationInstance is IHttpAsyncHandler)
{
IHttpAsyncHandler handler2 = (IHttpAsyncHandler) applicationInstance;
context.AsyncAppHandler = handler2;
handler2.BeginProcessRequest(context, this._handlerCompletionCallback, context);
}
else
{
applicationInstance.ProcessRequest(context);
this.FinishRequest(context.WorkerRequest, context, null);
}
}
catch (Exception exception)
{
context.Response.InitResponseWriter();
this.FinishRequest(wr, context, exception);
}
}
您可能感興趣的文章:
- 利用xmlhttp和adodb.stream加緩存技術(shù)下載遠程Web文件
- 用ASP VBS xmlhttp adodbstream下載和保存圖片的代碼
- 利用MSXML2.XmlHttp和Adodb.Stream采集圖片
- 使用asx3m與xstream配合解決flex與java利用httpservice傳遞xml數(shù)據(jù)問題
- php中突破基于HTTP_REFERER的防盜鏈措施(stream_context_create)
- php錯誤提示failed to open stream: HTTP request failed!的完美解決方法
- 利用stream實現(xiàn)一個簡單的http下載器
相關(guān)文章
HttpResponse的Output與OutputStream、Filter關(guān)系與區(qū)別介紹
在網(wǎng)上經(jīng)??匆娪羞@樣的代碼HttpResponse response = HttpContext.Current.Response;現(xiàn)在我也來說說這幾個東東是什么吧2012-11-11
深入分析XmlSerializer對象的Xml序列化與反序列化的示例詳解
本篇文章是對XmlSerializer 對象的Xml序列化與反序列化的應(yīng)用進行了詳細的分析介紹,需要的朋友參考下2013-05-05
ASP.NET?Core使用功能開關(guān)控制路由訪問操作
這篇文章主要介紹了ASP.NET?Core使用功能開關(guān)控制路由訪問操作,而對于一些試驗性的功能,我們并不希望用密碼去控制是否允許訪問,而是想用一種開關(guān)的方式開放,下面文章我們就來試著實現(xiàn)這個功能,需要的小伙伴可以參考一下2022-02-02
ABP入門系列應(yīng)用BootstrapTable表格插件
Bootstrap table是一個開源的輕量級功能非常豐富的前端表格插件。下面通過本文給大家介紹ABP入門系列應(yīng)用BootstrapTable表格插件,感興趣的朋友一起學(xué)習(xí)吧2017-03-03
.NET?中配置從xml轉(zhuǎn)向json方法示例詳解
這篇文章主要為大家介紹了.NET?中配置從xml轉(zhuǎn)向json方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
ASP.NET webUploader上傳大視頻文件相關(guān)web.config配置
本文主要介紹了webUploader上傳大視頻文件相關(guān)web.config的配置。具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01
ASP.NET?Core使用功能開關(guān)控制路由訪問操作(續(xù))
這篇文章主要介紹了ASP.NET?Core使用功能開關(guān)控制路由訪問操作的(續(xù)),上一篇文章我們已經(jīng)介紹過一部份該相關(guān)內(nèi)容,??在本文,我們可以判斷當前路由地址是否為調(diào)試地址,讓評估返回真,需要的小伙伴可以參考一下2022-02-02

