locked
Request.Filter RRS feed

  • Question

  • User604104433 posted

     Hi,all
    I have some problem for changing 'Request.InputStream' with 'Request.Filter' or any other solution
    how can I change my Request  InputStream ? before sending data to server.


    example:I wanna Replace the value "txt1" of asp.net textbox contorl coming from 'Request.InputStream',  
    by "txt2" that created in HttpModule Begin_Request handler ."txt2" must use instead of "txt1" before sending data to server!
    is there any solution to change stream of 'Request.InputStream' by 'Request.Filter'???

    Saturday, February 21, 2009 7:58 AM

Answers

All replies

  • User-1136466523 posted

    Hi,

    From your description, you're wanting to do some certain modification on the Request's InputStream and also you found using the Request Filter
    HttpModule not worked, yes? If there is anything I misunderstood, please feel free to let me know.

    Based on my experience, generally if we'd like to do some general operations on the Request's InputStream, the only way is just the Filter
    httpMoudle. But this way may be somewhat complex because that means we need to directly deal with the raw byte stream of the Request. Here are some tech articles on using the HTTPMODULE to generate Fitler:


    #Intercepting Filter
    http://msdn.microsoft.com/library/en...erceptingFilte
    r.asp?frame=true

    #Implementing Intercepting Filter in ASP.NET Using HTTP Module
    http://msdn.microsoft.com/library/en...erceptingFilte
    rInASP.asp?frame=true

    #Filtering HTTP Requests with .NET
    http://www.ondotnet.com/pub/a/dotnet...ttpfilter.html

    #HttpRequest.Filter Property
    http://msdn.microsoft.com/library/en...WebHttpRequest
    ClassFilterTopic.asp?frame=true

    #Build an ASP.NET HttpRequest Log Filter
    http://www.eggheadcafe.com/articles/20030813.asp

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 25, 2009 9:51 PM
  • User673358515 posted

    I was wondering if you have any advice on a problem I am seeing.  I am creating an HttpModule where in the OnBeginRequest, I create a wrapper stream passing in the Request.Filter (I tried to pass in the Request.InputStream but when I run my application, I get an HttpException saying The request filter is not valid) into it and then set that wrapper stream as the filter doing something like Request.Filter = myWrapper.  In my wrapper class I implement all the Stream methods, with the main one being of course the Read method where I was hoping to grab the bytes that are read and then write them out somewhere else.  What is odd is I was expecting that when the InputStream was accessed (which I was doing in my test app in its Application_Begin_Request, it would be indirectly using my filter, but it does not.  Is there anything else I need to enable in order for my wrapper class to be enabled as the Filter when InputStream is use on the Request?

    Thanks in advance for any advice!

    public class RequestLoggingFilter : IHttpModule
            protected void OnBeginRequest(object sender, EventArgs ea) {
                HttpApplication application = (HttpApplication)sender;
                HttpRequestLog httpRequestLog = new HttpRequestLog();
                application.Context.Items.Add("HttpRequestLog", httpRequestLog);
                WrapperInputStream wrapperInputStream = new WrapperInputStream(application.Request.Filter);
                httpRequestLog.WrapperInputStream = wrapperInputStream;
                application.Request.Filter = wrapperInputStream;
            }
    .....
    }
    
    
    public class WrapperInputStream : Stream{
    
            public WrapperInputStream(Stream inputStream) {
                mInputStream = inputStream;
                if (inputStream.Length > 0) {
                    mOutputStream = new MemoryStream((int)inputStream.Length);
                }
                else {
                    mOutputStream = new MemoryStream();
                }
            }
    
            public override int Read(byte[] buffer, int offset, int count) {
                int readBytes = mInputStream.Read(buffer, offset, count);
                if (readBytes > 0) {
                    mOutputStream.Write(buffer, offset, readBytes);
                    mDataCaptureStarted = true;
                    if (readBytes == count) {
                        mDataCaptureComplete = true;
                    }
                }
                else {
                    mDataCaptureComplete = true;
                }
                return readBytes;
            }
    ...
    }
    
    public class Global : System.Web.HttpApplication {
            protected void Application_BeginRequest(object sender, EventArgs e) {
                String mBody = "";
                Stream stream = ((HttpApplication)sender).Request.InputStream;
                int length = (int)stream.Length;
                logger.Info(length);
                try {
                    byte[] bytes = new byte[length];
                    stream.Read(bytes, 0, length);
                    foreach (byte b in bytes) {
                        mBody += (Char)b;
                    }
                    //StreamReader reader = new StreamReader(((HttpApplication)sender).Request.InputStream);
                    //mBody = reader.ReadToEnd();
                    //reader.Close();
                }
                catch (Exception ex) {
                    mBody = "Error decoding body: " + ex.Message;
                }
                logger.Info(mBody);
            }
    ...
    }


     

    Thursday, June 25, 2009 10:33 AM
  • User-789305915 posted

    I have come across the same problem. Request.Filter is just not working, my custom stream class code is never called, though for Response.Filter it works ok.

    Friday, July 10, 2009 2:37 AM
  • User-789305915 posted

    I have got it working, see example here:

    http://msdn.microsoft.com/en-us/library/system.web.httprequest.filter.aspx

    If you need to do filtering in http module, use the code below.

    My problem was that before attaching the filter I checked for HttpContext.Current.Request.InputStream.Length > 0 which must have had a side effect of reading the input stream and bypassing filtering.

     

       public class FilterHttpModule : System.Web.IHttpModule
        {
            void IHttpModule.Dispose()
            {
            }
    
            void IHttpModule.Init(HttpApplication context)
            {
                context.BeginRequest += this.BeginRequestHandler;
            }
    
            private void BeginRequestHandler(object sender, EventArgs args)
            {
                HttpContext.Current.Request.Filter = new QQQ1(HttpContext.Current.Request.Filter);
                HttpContext.Current.Request.Filter = new QQQ2(HttpContext.Current.Request.Filter);
            }
        }


     

    Friday, July 10, 2009 4:10 AM