当前位置: 移动技术网 > IT编程>开发语言>.net > HttpResponse的Output与OutputStream、Filter关系与区别介绍

HttpResponse的Output与OutputStream、Filter关系与区别介绍

2017年12月12日  | 移动技术网IT编程  | 我要评论

凤凰卫视军情连连看,中国下山兰花拍卖网,石家庄别墅装修

在网上经常看见有这样的代码

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网