我已经写了一个httpModule做一些站点的跟踪.我们网站上的一些旧的.aspx页面是硬编码的,没有真正的控件,但它们是.aspx文件,因此我们的模块在请求时仍然运行.
我的模块的处理程序附加到PostRequestHandlerExecute,所以我相信将被发送回请求者应该已经确定了.
我需要能够提取标题标签中的任何字符串.
因此,如果
<Title>Chunky Bacon</Title>
在最终呈现的HTML中发送到请求者.然后我想要“Chunky Bacon”.
想法?
解决方法 有趣的小挑战以下是代码:
StreamWatcher.cs
public class StreamWatcher : Stream { private Stream _base; private MemoryStream _memoryStream = new MemoryStream(); public StreamWatcher(Stream stream) { _base = stream; } public overrIDe voID Flush() { _base.Flush(); } public overrIDe int Read(byte[] buffer,int offset,int count) { return _base.Read(buffer,offset,count); } public overrIDe voID Write(byte[] buffer,int count) { _memoryStream.Write(buffer,count); _base.Write(buffer,count); } public overrIDe string ToString() { return EnCoding.UTF8.GetString(_memoryStream.ToArray()); } #region Rest of the overrIDes public overrIDe bool CanRead { get { throw new NotImplementedException(); } } public overrIDe bool CanSeek { get { throw new NotImplementedException(); } } public overrIDe bool CanWrite { get { throw new NotImplementedException(); } } public overrIDe long Seek(long offset,SeekOrigin origin) { throw new NotImplementedException(); } public overrIDe voID SetLength(long value) { throw new NotImplementedException(); } public overrIDe long Length { get { throw new NotImplementedException(); } } public overrIDe long position { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } #endregion }
TitleModule.cs
public class TitleModule : IhttpModule{ public voID dispose() { } private static Regex regex = new Regex(@"(?<=<Title>)[\w\s\r\n]*?(?=</Title)",RegexOptions.Compiled | RegexOptions.IgnoreCase); private StreamWatcher _watcher; public voID Init(httpApplication context) { context.BeginRequest += (o,e) => { _watcher = new StreamWatcher(context.Response.Filter); context.Response.Filter = _watcher; }; context.EndRequest += (o,e) => { string value = _watcher.ToString(); Trace.Writeline(regex.Match(value).Value.Trim()); }; }}总结
以上是内存溢出为你收集整理的c# – 如何从HttpModule中检索响应html?全部内容,希望文章能够帮你解决c# – 如何从HttpModule中检索响应html?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)