Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6138357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:51:40+00:00 2026-05-23T17:51:40+00:00

Assuming I am doing webservice request from the client. The request is made by

  • 0

Assuming I am doing webservice request from the client. The request is made by POST with json parameters.
In the server, Context.Request.RawUrl hold the request url. But what holds the request json data?

I need this property in order to log requests.

Here is the code for the http module:

  public class RequestResponseLoggerModule : IHttpModule
    {
        //private static Logger logger = LogManager.GetCurrentClassLogger();

        #region ResponseCaptureStream

        private class ResponseCaptureStream : Stream
        {
            private readonly Stream _streamToCapture;
            private readonly Encoding _responseEncoding;

            public string StreamContent { get; private set; }            

            public ResponseCaptureStream(Stream streamToCapture, Encoding responseEncoding)
            {
                _responseEncoding = responseEncoding;
                _streamToCapture = streamToCapture;
            }

            public override bool CanRead { get { return _streamToCapture.CanRead; } }
            public override bool CanSeek { get { return _streamToCapture.CanSeek; } }
            public override bool CanWrite { get { return _streamToCapture.CanWrite; } }
            public override long Length { get { return _streamToCapture.Length; } }
            public override long Position
            {
                get { return _streamToCapture.Position; }
                set { _streamToCapture.Position = value; }
            }


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

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

            public override long Seek(long offset, SeekOrigin origin)
            {
                return _streamToCapture.Seek(offset, origin);
            }

            public override void SetLength(long value)
            {
                _streamToCapture.SetLength(value);
            }

            public override void Write(byte[] buffer, int offset, int count)
            {
                StreamContent += _responseEncoding.GetString(buffer);
                _streamToCapture.Write(buffer, offset, count);
            }

            public override void Close()
            {
                _streamToCapture.Close();
                base.Close();
            }
        }

        #endregion

        #region IHttpModule Members

        private HttpApplication _context;
        public void Dispose()
        {

        }

        private static string Extensions_Key = "RequestResponseLoggerModule.Extensions";
        private static string Files_Key = "RequestResponseLoggerModule.Files";
        private static string Request_Key = "RequestResponseLoggerModule.Request";
        private static string Response_Key = "RequestResponseLoggerModule.Response";

        private IEnumerable<string> _Extenstions = new string[] { };
        private IEnumerable<string> _Files = new string[] { };
        private bool _LogAlwaysRequest = false;
        private bool _LogAlwaysResponse = false;

        public void Init(HttpApplication context)
        {
            _Extenstions = ConfigurationManager.AppSettings[Extensions_Key].ToLower().Split(',').
                Select(x => x.Trim()).ToArray();
            _Files = ConfigurationManager.AppSettings[Files_Key].ToLower().Split(',').
                Select(x => x.Trim()).ToArray();
            _LogAlwaysRequest = ConfigurationManager.AppSettings[Request_Key] == "ALWAYS";
            _LogAlwaysResponse = ConfigurationManager.AppSettings[Response_Key] == "ALWAYS";

            _context = context;

            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
            context.PreSendRequestContent += new EventHandler(context_PreSendRequestContent);
        }

        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            _context.Response.Filter = new ResponseCaptureStream(_context.Response.Filter, _context.Response.ContentEncoding);
        }

        void context_PreSendRequestContent(object sender, EventArgs e)
        {
            bool isMatch = false;
            if (_Extenstions.Count() > 0)
            {
                string ext = VirtualPathUtility.GetExtension(_context.Request.FilePath).
                    Substring(1).ToLower();
                if (_Extenstions.Contains(ext))
                {
                    isMatch = true;
                }
            }
            if (_Files.Count() > 0)
            {
                string fileName = VirtualPathUtility.GetFileName(_context.Request.FilePath).                   ToLower();
                if (_Files.Contains(fileName))
                {
                    isMatch = true;
                }
            }

            if (_LogAlwaysRequest || isMatch)
            {
                string requestText = _context.Request.RawUrl; <------------------

                //log the responseText, but this reurns just the url while I need the data of the webservice call
            }
            if (_LogAlwaysResponse || isMatch)
            {
                ResponseCaptureStream filter = _context.Response.Filter as ResponseCaptureStream;
                if (filter != null)
                {
                    string responseText = filter.StreamContent;
                    //log the responseText
                }
            }        
        }

        #endregion
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:51:41+00:00Added an answer on May 23, 2026 at 5:51 pm
    public class RequestResponseLoggerModule : IHttpModule
    {
        //private static Logger logger = LogManager.GetCurrentClassLogger();
    
        #region CaptureStream
    
        private class CaptureStream : Stream
        {
            private readonly Stream _streamToCapture;
            private readonly Encoding _encoding;
    
            public string StreamContent { get; private set; }
    
            public CaptureStream(Stream streamToCapture, Encoding encoding)
            {
                _encoding = encoding;
                _streamToCapture = streamToCapture;
            }
    
            public override bool CanRead { get { return _streamToCapture.CanRead; } }
            public override bool CanSeek { get { return _streamToCapture.CanSeek; } }
            public override bool CanWrite { get { return _streamToCapture.CanWrite; } }
            public override long Length { get { return _streamToCapture.Length; } }
            public override long Position
            {
                get { return _streamToCapture.Position; }
                set { _streamToCapture.Position = value; }
            }
    
    
            public override void Flush()
            {
                _streamToCapture.Flush();
            }           
    
            public override int Read(byte[] buffer, int offset, int count)
            {
                return _streamToCapture.Read(buffer, offset, count);
            }
    
            public override long Seek(long offset, SeekOrigin origin)
            {
                return _streamToCapture.Seek(offset, origin);
            }
    
            public override void SetLength(long value)
            {
                _streamToCapture.SetLength(value);
            }
    
            public override void Write(byte[] buffer, int offset, int count)
            {
                StreamContent += _encoding.GetString(buffer);
                _streamToCapture.Write(buffer, offset, count);
            }
    
            public override void Close()
            {
                _streamToCapture.Close();
                base.Close();
            }
        }
    
        #endregion
    
        #region IHttpModule Members
    
        private HttpApplication _context;
        public void Dispose()
        {
    
        }
    
        private static string Extensions_Key = "RequestResponseLoggerModule.Extensions";
        private static string Files_Key = "RequestResponseLoggerModule.Files";
        private static string Request_Key = "RequestResponseLoggerModule.Request";
        private static string Response_Key = "RequestResponseLoggerModule.Response";
    
        private IEnumerable<string> _Extenstions = new string[] { };
        private IEnumerable<string> _Files = new string[] { };
        private bool _LogAlwaysRequest = false;
        private bool _LogAlwaysResponse = false;
        private bool _IsMatch = false;
    
        public void Init(HttpApplication context)
        {
            _Extenstions = ConfigurationManager.AppSettings[Extensions_Key].ToLower().Split(',').
                Select(x => x.Trim()).ToArray();
            _Files = ConfigurationManager.AppSettings[Files_Key].ToLower().Split(',').
                Select(x => x.Trim()).ToArray();
            _LogAlwaysRequest = ConfigurationManager.AppSettings[Request_Key] == "ALWAYS";
            _LogAlwaysResponse = ConfigurationManager.AppSettings[Response_Key] == "ALWAYS";
    
            _context = context;
    
            context.BeginRequest += new EventHandler(context_BeginRequest);
            context.PreSendRequestContent += new EventHandler(context_PreSendRequestContent);
        }
    
        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            _context.Response.Filter = new CaptureStream(_context.Response.Filter, _context.Response.ContentEncoding);
        }
    
        void context_BeginRequest(object sender, EventArgs e)
        {
            if (_Extenstions.Count() > 0)
            {
                string ext = VirtualPathUtility.GetExtension(_context.Request.FilePath).
                    Substring(1).ToLower();
                if (_Extenstions.Contains(ext))
                {
                    _IsMatch = true;
                }
            }
            if (_Files.Count() > 0)
            {
                string fileName = VirtualPathUtility.GetFileName(_context.Request.FilePath).ToLower();
                if (_Files.Contains(fileName))
                {
                    _IsMatch = true;
                }
            }
    
            if (_LogAlwaysRequest || _IsMatch)
            {
                StreamReader reader = new StreamReader(_context.Request.InputStream);
                string requestText = reader.ReadToEnd();
                _context.Request.InputStream.Position = 0;
                //LOG requestText
            }
        }
    
        void context_PreSendRequestContent(object sender, EventArgs e)
        {
            if (_LogAlwaysResponse || _IsMatch)
            {
                CaptureStream filter = _context.Response.Filter as CaptureStream;
                if (filter != null)
                {
                    string responseText = filter.StreamContent;
                    //LOG responseText
                }
            }
        }
    
        #endregion
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming I'm doing something like this (from the Active Record Querying guide ) Item.transaction
Assuming network access is sporadic with no central server, what would be the best
Assuming I have an open source web server or proxy I can enhance, let's
Assuming you're doing CI , the title really says it all: What tools do
Assuming such a query exists, I would greatly appreciate the help. I'm trying to
Assuming you can't use LINQ for whatever reason, is it a better practice to
Assuming there are 5 items in the settings file ( MySetting1 to MySetting5 ),
Assuming I have only the class name of a generic as a string in
Assuming String a and b: a += b a = a.concat(b) Under the hood,
Assuming I'm trying to automate the installation of something on windows and I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.