Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to determine the size of Response.OutputStream?

How to determine the size of Response.OutputStream?

Scheduled Pinned Locked Moved ASP.NET
csharptutorialquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jszpila
    wrote on last edited by
    #1

    Hello all, I'm in the process of writing an HTTP Module that will determine the size of the response and display it. I'm pretty new the http module thing so there's probably a good way to do this that I haven't found yet, but I've tried everything I can think of. Here's what I have so far:

    public class ResponseSizeHttpModule : IHttpModule
    {
    public int RespSize;

        public void Init(HttpApplication application)
        {
            application.PreSendRequestHeaders += new EventHandler(this.ResponseCheck);
            application.EndRequest += new EventHandler(this.ResponseMessage);
        }
        
        private void ResponseCheck(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            **StreamReader reader = new StreamReader(context.Response.OutputStream);**
            
            reader.ReadToEnd();
            reader.Close();
            
            byte\[\] buffer = System.Text.Encoding.UTF8.GetBytes(reader.ToString());
    
            RespSize = buffer.Length;
        }
    
        private void ResponseMessage(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            context.Response.Write("Page size: " + RespSize + " bytes.");
        }
    
        public void Dispose(){}
    }
    

    So far, the message comes up but the page size is always 0, so that's not right. It's come to my attention that the bold part that OutputStream is write-only, if I understand this correctly. How would I go about intercepting what's being written to the OutputStream so I can determine its size? Like I said, my knowledge of this particular facet of .Net isn't all that great, so if anyone can recommend a good method or alternative, I'd be glad to hear it. Thanks in advance, Jan

    ------------------- abort, retry, fail?

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups