C#代码,功能为拦截http请求,并篡改其中任何内容

可以对http最终输出内容做任意的替换,代码如下:

using System;
using System.Collections;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;

namespace Cnlamar.Web.HttpModule
{
    public class TextReplace : IHttpModule
    {

        public virtual void Dispose()
        {
        }

        public virtual void Init(HttpApplication context)
        {
            context.ReleaseRequestState += new EventHandler(this.context_ReleaseRequestState);
        }

        public void context_ReleaseRequestState(object sender, EventArgs e)
        {
            HttpApplication httpApplication = (HttpApplication)sender;
            if (httpApplication.Response.ContentType.ToLower().IndexOf("image") <= -1)
            {
                TextReplaceFilter textReplaceFilter = new TextReplaceFilter(httpApplication.Response.Filter);
                textReplaceFilter.Encoding = System.Text.Encoding.GetEncoding(httpApplication.Response.ContentEncoding.BodyName);
                textReplaceFilter.ReplaceRules = TextReplaceConfiguration.GetConfig().Rules;
                httpApplication.Response.Filter = textReplaceFilter;
            }
        }
    }

    class TextReplaceFilter : Stream
    {
        public bool IsRestore = false;

        private Stream m_stream;

        private long m_position;

        private TextReplaceRulesCollection _ReplaceRules;

        


        public TextReplaceRulesCollection ReplaceRules
        {
            get
            {
                return _ReplaceRules;
            }

            set
            {
                _ReplaceRules = value;
            }
        }

        public Encoding Encoding
        {
            get
            {
                return _Encoding;
            }

            set
            {
                _Encoding = value;
            }
        }
        private Encoding _Encoding = Encoding.UTF8;

        public override bool CanRead
        {
            get
            {
                return true;
            }
        }

        public override bool CanSeek
        {
            get
            {
                return false;
            }
        }

        public override bool CanWrite
        {
            get
            {
                return false;
            }
        }

        public override long Length
        {
            get
            {
                return 0;
            }
        }

        public override long Position
        {
            get
            {
                return m_position;
            }

            set
            {
                m_position = value;
            }
        }

        public TextReplaceFilter(Stream InputStream)
        {
            m_stream = InputStream;
        }

        public override long Seek(long offset, SeekOrigin direction)
        {
            return 0;
        }

        public override void SetLength(long length)
        {
            m_stream.SetLength(length);
        }

        public override void Close()
        {
            m_stream.Close();
        }

        public override void Flush()
        {
            m_stream.Flush();
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return m_stream.Read(buffer, offset, count);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            string str = Encoding.GetString(buffer, offset, count);
            IEnumerator iEnumerator = ReplaceRules.GetEnumerator();
            try
            {
                while (iEnumerator.MoveNext())
                {
                    TextReplaceRules textReplaceRules = (TextReplaceRules)iEnumerator.Current;
                    str = str.Replace(textReplaceRules.OriginText, textReplaceRules.ResultText);
                }
            }
            finally
            {
                IDisposable iDisposable = iEnumerator as IDisposable;
                if (iDisposable != null)
                {
                    iDisposable.Dispose();
                }
            }
            byte[] bs = Encoding.GetBytes(str);
            m_stream.Write(bs, offset, (int)bs.Length);
        }
    }

    #region Config
    public class TextReplaceRules
    {
        private string _OriginText;

        private string _ResultText;


        public string OriginText
        {
            get
            {
                return _OriginText;
            }

            set
            {
                _OriginText = value;
            }
        }

        public string ResultText
        {
            get
            {
                return _ResultText;
            }

            set
            {
                _ResultText = value;
            }
        }
    }

    [SerializableAttribute()]
    public class TextReplaceRulesCollection : CollectionBase
    {

        public TextReplaceRules this[int index]
        {
            get
            {
                return (TextReplaceRules)base.InnerList[index];
            }

            set
            {
                base.InnerList[index] = value;
            }
        }

        public virtual void Add(TextReplaceRules r)
        {
            base.InnerList.Add(r);
        }
    }

    [XmlRootAttribute("TextReplaceConfig")]
    [SerializableAttribute()]
    public class TextReplaceConfiguration
    {
        private TextReplaceRulesCollection rules;


        public TextReplaceRulesCollection Rules
        {
            get
            {
                return rules;
            }

            set
            {
                rules = value;
            }
        }

        public static TextReplaceConfiguration GetConfig()
        {
            if (HttpContext.Current.Cache["TextReplaceConfig"] == null)
            {
                TextReplaceConfiguration textReplaceConfiguration1 = (TextReplaceConfiguration)ConfigurationSettings.GetConfig("TextReplaceConfig");
                HttpContext.Current.Cache.Insert("TextReplaceConfig", ConfigurationSettings.GetConfig("TextReplaceConfig"));
            }
            TextReplaceConfiguration textReplaceConfiguration2 = (TextReplaceConfiguration)HttpContext.Current.Cache["TextReplaceConfig"];
            return textReplaceConfiguration2;
        }
    }

    public class TextReplaceSerializerSectionHandler : IConfigurationSectionHandler
    {

        public virtual object Create(object parent, object configContext, XmlNode section)
        {
            return new XmlSerializer(typeof(TextReplaceConfiguration)).Deserialize(new XmlNodeReader(section));
        }
    }
    #endregion
}

你可能感兴趣的:(http)