当前位置: 首页 > 编程语言 > C#

IIS7托管模块无法获取内容长度或已发送字节共享

时间:2023-04-10 17:18:00 C#

IIS7托管模块无法获取内容长度或已发送字节定制加工。我正在尝试为IIS7更新它,但我遇到了问题。IIS7事件似乎都无法访问内容长度、发送的字节数或任何允许我计算内容长度或发送的字节数的数据。(我知道Content-Length标头和发送的字节是不一样的,但两者都可以用于此目的。)据我所知,HTTP.SYS在托管模块完成执行后添加Content-Length标头。现在我有一个在EndRequest上运行的事件处理程序。如果我可以获得输出流,我可以自己弄清楚我需要什么,但托管管道似乎也不可访问。有什么方法可以获取托管管道中发送的内容长度或字节数吗?如果做不到这一点,是否有某种方法可以计算托管管道中可用对象发送的内容长度或字节数?要获取发送的字节,您可以使用HttpResponse.Filter属性。正如MSDN文档所说,此属性获取或设置一个包装过滤器对象,用于在传输之前修改HTTP实体主体。您可以创建一个新的System.IO.Stream来包装现有的HttpResponse.Filter流,并在传递之前计算传递给Write方法的字节数。例如:以上是C#学习教程:IIS7托管模块无法获取Content-Length或发送的字节数。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注——context.EndRequest+=OnEndRequest;}voidOnBeginRequest(objectsender,EventArgse){varapplication=(Httender.Replication.application)sFilter=newContentLengthFilter(application.Response.Filter);}voidOnEndRequest(objectsender,EventArgse){varapplication=(HttpApplication)sender;varcontentLengthFilter=(ContentLengthFilter)application.Response.FilterWritth;}publicvoidDispose(){}}publicclassContentLengthFilter:Stream{privatereadonlyStream_responseFilter;publicintBytesWritten{得到;放;}publicContentLengthFilter(StreamresponseFilter){_responseFilter=responseFilter;}publicoverridevoidFlush(){_responseFilter.Flush();}publicoverridelongSeek(longoffset,SeekOriginorigin){return_responseFilter.Seek(offset,origin);}publicoverridevoidSetLength(longvalue){_responseFilter.SetLength(value);}publicoverrideintRead(byte[]buffer,intoffset,intcount){return_responseFilter.读取(缓冲区,偏移量,计数);}publicoverridevoidWrite(byte[]buffer,intoffset,intcount){BytesWritten+=count;_responseFilter.Write(缓冲区,偏移量,计数);}publicoverrideboolCanRead{get{return_responseFilter.CanRead;}}publicoverrideboolCanSeek{get{return_responseFilter.CanSeek;;}}publicoverridelongLength{get{return_responseFilter.Length;}}publicoverridelongPosition{get{return_responseFilter.Position;}设置{_responseFilter.Position=值;}}}如有侵权,请点击右边联系管理员删除如需转载请注明出处: