Question

I'm looking for a way to get the number of bytes sent up to a certain point in an ASP.NET 2.0 response so that I can write this number into the response. Something like:

Page.aspx:

<script runat="server">
    private string GetResponseSize()
    {
        int nCurrentResponseSize = ...; // what goes here?
        return ( nCurrentResponseSize.ToString() );
    }
</script>
<html>
  <head runat="server"><title>test</title></head>
  <body>
    <div>Size 1: <%= GetResponseSize() %></div>
    <br />
    <div>Size 2: <%= GetResponseSize() %></div>
  </body>
</html>

The problem is that HttpResponse.OutputStream.Length is not supported (NetworkStream doesn't support it).

I could implement an HtmlTextWriter to override the page's default one and count bytes but something inside ASP.NET should already have the count and I'm trying to see if it's possible to get it.

What I'd like to avoid is solutions that involve getting all the data and then parsing it to a certain point to find the size up to that point. So far most of my searches only turned up these kind of solutions.

Was it helpful?

Solution

Using dotPeek I found that HttpResponse has an internal GetBufferedLength method that is used to calculate the size of a control for tracing:

int bufferedLength1 = httpContext.Response.GetBufferedLength();
this.RenderControlInternal(writer, adapter);
int bufferedLength2 = httpContext.Response.GetBufferedLength();

It's not ideal, but you could call this method via reflection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top