一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - HttpResponse的Output與OutputStream、Filter關系與區別介紹

HttpResponse的Output與OutputStream、Filter關系與區別介紹

2019-10-09 14:53asp.net教程網 ASP.NET教程

在網上經常看見有這樣的代碼HttpResponse response = HttpContext.Current.Response;現在我也來說說這幾個東東是什么吧

在網上經常看見有這樣的代碼 

HttpResponse response = HttpContext.Current.Response; 
response.Filter = new PageFilter(response.Filter); 

來攔截輸出流,自己也做個類似的東東,如asp.net中 js 合并 壓縮,現在我也來說說這幾個東東是什么吧,需要大家對asp.net的生命周期比較熟悉,如不熟悉的朋友建議先看看ASP.NET 請求處理流程 ASP.NET管線與應用程序生命周期 

首先我們來看看這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。現在我們可以解釋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屬性設置調用了HttpWriter類的InstallFilter方法,而獲取調用了該類的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屬性設置的流,即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來調用

復制代碼代碼如下:


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); 

}

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 红楼影视h38bar在线线播放 | 日本妇人成熟免费观看18 | 亚洲美女爱爱 | 亚洲精品卡一卡2卡3卡4卡 | 国产一区日韩二区欧美三区 | 成熟女人50岁一级毛片不卡 | 桃乃木香奈作品在线观看 | 精品在线免费播放 | 精品国产品香蕉在线观看 | 国产caonila在线观看 | 99任你躁精品视频 | 国产精品久久一区 | 女人用粗大自熨喷水在线视频 | 亚洲XXX午休国产熟女屁 | 亚洲国产综合另类视频 | 学生小泬无遮挡女HD | 四虎b7s22c0m| 12-14娇小videos | 日韩一级精品视频在线观看 | 青青草99| 久草在线草a免费线看 | 日韩日日操 | 经典千人斩一区二区视频 | 色综合久久综合网欧美综合网 | 交换年轻夫妇HD中文字幕 | 冰漪丰满大乳人体图片欣赏 | 男男羞羞视频网站国产 | 亚洲精品乱码蜜桃久久久 | 91污污视频 | 91制片厂 果冻传媒 天美传媒 | 男人资源站 | 91精品国产高清久久久久 | a v在线男人的天堂观看免费 | gay中国| 无限好资源免费观看 | 亚洲精品国产福利片 | 禁忌高h| 丝瓜秋葵番茄绿巨人在线观看 | 疯狂刺激的3p国产在线 | 四虎影院入口 | 国产手机在线αⅴ片无码观看 |