zoukankan      html  css  js  c++  java
  • 怎样修改Response中的内容

    重写Stream

    public class CatchTextStream : Stream
        {
            private Stream output;
            public CatchTextStream(Stream s)
            {
                output = s;
            }
            public override bool CanRead
            {
                get { return output.CanRead; }
            }
    
            public override bool CanSeek
            {
                get { return output.CanSeek; }
            }
    
            public override bool CanWrite
            {
                get { return output.CanWrite; }
            }
    
            public override void Flush()
            {
                output.Flush();
            }
    
            public override long Length
            {
                get { return output.Length; }
            }
    
            public override long Position
            {
                get { return output.Position; }
                set { output.Position = value; }
            }
    
            public override int Read(byte[] buffer, int offset, int count)
            {
                return output.Read(buffer, offset, count);
            }
    
            public override long Seek(long offset, SeekOrigin origin)
            {
                return output.Seek(offset, origin);
            }
    
            public override void SetLength(long value)
            {
                output.SetLength(value);
            }
    
            public override void Write(byte[] buffer, int offset, int count)
            {
                StringComparison ignore = StringComparison.CurrentCultureIgnoreCase;
                if (HttpContext.Current != null)
                {
                    HttpContext context = HttpContext.Current;
                    if (context.Response.ContentType.Equals("text/html", ignore))
                    {
                        Encoding encoding = context.Response.ContentEncoding;
    
                        
                        
                        string html = encoding.GetString(buffer, offset, count);
    //在这里可以用你熟悉的方法修改html
                        byte[] bytes = encoding.GetBytes(html);
                        output.Write(bytes, 0, bytes.Length);
                    }
                    else
                        output.Write(buffer, offset, count);
                }
            }
        }

    然后把Response的Stream给换掉,偷梁换柱,哈哈……

    CatchTextStream responseStream = new CatchTextStream(Response.Filter);
                        Response.Filter = responseStream;

    为什么Response的Stream不能读呢?

    这样就不行New StreamReader(Response.Filter);运行时报错,还请大家指点

  • 相关阅读:
    《病理学》读书笔记
    《药理学》读书笔记
    《人格心理学》读书笔记
    《普通心理学》读书笔记
    UI进阶 跳转系统设置相关界面的方法
    安装cocoapods
    CocoaPods 添加第三方库报错
    Objective-c setObject:forKey:和setValue:forKey:的区别
    解析稍微复杂一点的数据
    获取当前屏幕显示的viewcontroller
  • 原文地址:https://www.cnblogs.com/LLLLoveLLLLife/p/3230472.html
Copyright © 2011-2022 走看看