asp.net 过滤器 过滤</html>后面的代码
最近一段时间asp.net网站首页总是挂一些第三方的代码在</html>后面,如
</form>
</body>
</html>
<div style="position: absolute; top: -123px;left: -123px;">
<li><a href='/zqcj'>baidu</a></li>
……
过滤器 怎么写才过滤所有站点所有</html>不要的信息。
如现成实例,发一份至我邮箱:361654972@qq.com
十分感谢! --------------------编程问答-------------------- 关注! --------------------编程问答-------------------- 已经解决http://rcdos.net/Article.aspx?Para=158 --------------------编程问答-------------------- 备注mark --------------------编程问答--------------------
/*----------------------------------------------------------------
// Copyright (C) 2010
// 版权所有。
//
// 文件名:_OutputFilter
// 文件功能描述:过滤器(Session\权限\等等)
//
//
// 创建标识:胡世书
//
// 修改标识:2011.10.22
// 修改描述:新增在</html>结束后的代码全部过滤
//
//标识:胡世书
// 修改标识:
// 修改描述: ----------------------------------------------------------------*/
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
namespace Shove
{
public class OutputFilter : IHttpModule
{
private HttpApplication _contextApplication;
public void Init(HttpApplication context)
{
_contextApplication = context;
//绑定事件,在对此请求处理过程全部结束后进行过滤操作
context.ReleaseRequestState += new EventHandler(context_ReleaseRequestState);
}
public void Dispose()
{
_contextApplication = null;
_contextApplication.Dispose();
}
/**/
/// <summary>
/// 对此HTTP请求处理的过程全部结束
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void context_ReleaseRequestState(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
//这里需要针对ASPX页面进行拦截
string[] temp = application.Request.CurrentExecutionFilePath.Split('.');
if (temp.Length > 0 && temp[temp.Length - 1].ToLower() == "aspx")
{
//装配过滤器
application.Response.Filter = new RawFilter(application.Response.Filter);
//绑定过滤器事件
//RawFilter filter = (RawFilter)application.Response.Filter;
//filter.OnRawDataRecordedEvent += new EventHandler<RawDataEventArgs>(filter_OnRawDataRecordedEvent);
}
}
/**/
/// <summary>
/// 当原始数据采集到以后,入库
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void filter_OnRawDataRecordedEvent(object sender, RawDataEventArgs e)
{
string allcode = e.SourceCode;//allcode就是截获的替换前的整个page页面的html
//WapSite.SiteDataClass wapdata = new WapSite.SiteDataClass();
//wapdata.WriteRawDataLog(allcode);
}
}
public class RawDataEventArgs : EventArgs
{
private string sourceCode;
public RawDataEventArgs(string SourceCode)
{
sourceCode = SourceCode;
}
public string SourceCode
{
get { return sourceCode; }
set { sourceCode = value; }
}
}
//自定义过滤器
public class RawFilter : Stream
{
Stream responseStream;
long position;
StringBuilder responseHtml;
/**/
/// <summary>
/// 当原始数据采集成功后激发。
/// </summary>
public event EventHandler<RawDataEventArgs> OnRawDataRecordedEvent;
public RawFilter(Stream inputStream)
{
responseStream = inputStream;
responseHtml = new StringBuilder();
}
//实现Stream 虚方法Filter Overrides
public override bool CanRead
{
get
{
return true;
}
}
public override bool CanSeek
{
get
{
return true;
}
}
public override bool CanWrite
{
get
{
return true;
}
}
public override void Close()
{
responseStream.Close();
}
public override void Flush()
{
responseStream.Flush();
}
public override long Length
{
get
{
return 0;
}
}
public override long Position
{
get
{
return position;
}
set
{
position = value;
}
}
public override int Read(byte[] buffer, int offset, int count)
{
return responseStream.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
return responseStream.Seek(offset, origin);
}
public override void SetLength(long length)
{
responseStream.SetLength(length);
}
//关键的点,在HttpResponse 输入内容的时候,一定会调用此方法输入数据,所以要在此方法内截获数据
//public override void Write(byte[] buffer, int offset, int count)
//{
// string strBuffer = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);
// //采用正则,检查输入的是否有页面结束符</html>
// Regex eof = new Regex("</html>", RegexOptions.IgnoreCase);
// if (!eof.IsMatch(strBuffer))
// {
// //页面没有输出完毕,继续追加内容
// responseHtml.Append(strBuffer);
// }
// else
// {
// //页面输出已经完毕,截获内容
// responseHtml.Append(strBuffer);
// string finalHtml = responseHtml.ToString();
// //激发数据已经获取事件
// OnRawDataRecordedEvent(this, new RawDataEventArgs(finalHtml));
// //继续传递要发出的内容写入流
// byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(finalHtml);
// responseStream.Write(data, 0, data.Length);
// }
//}
//关键的点,在HttpResponse 输入内容的时候,一定会调用此方法输入数据,所以要在此方法内截获数据
public override void Write(byte[] buffer, int offset, int count)
{
string strBuffer = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);
//采用正则,检查输入的是否有页面结束符</html>
Regex eof = new Regex("</html>", RegexOptions.IgnoreCase);
if (!eof.IsMatch(strBuffer))
{
//页面没有输出完毕,继续追加内容
responseHtml.Append(strBuffer);
}
else
{
//页面输出已经完毕,截获内容
responseHtml.Append(strBuffer);
string finalHtml = responseHtml.ToString();
//激发数据已经获取事件
//OnRawDataRecordedEvent(this, new RawDataEventArgs(finalHtml));//finalHtml就是源文件内容
//继续传递要发出的内容写入流
int iCount = finalHtml.ToLower().LastIndexOf("</html>") + 7;
byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(finalHtml.Substring(0, iCount));
responseStream.Write(data, 0, data.Length);
}
}
}
}
//引用 Shove
//
//在web.config 文件中加载
//在 <system.web>中加入
<httpModules>
<add name="OutputFilter1Html" type="Shove.OutputFilter" />
</httpModules>
Mark+1 --------------------编程问答-------------------- mark and up
补充:.NET技术 , ASP.NET