如何获得HttpResponse大小 [英] How to get HttpResponse size

查看:532
本文介绍了如何获得HttpResponse大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来获取发送到ASP.NET 2.0响应中特定点的字节数,以便可以将此数字写入响应中.像这样:

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:

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>

问题是不支持HttpResponse.OutputStream.Length( NetworkStream 不支持.)

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

我可以实现 HtmlTextWriter 覆盖页面的默认字节并计数字节,但是ASP.NET中的某些内容应该已经有了计数,我正在尝试查看是否有可能获得计数.

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.

推荐答案

使用dotPeek,我发现HttpResponse具有内部的GetBufferedLength方法,该方法用于计算用于跟踪的控件的大小:

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.

这篇关于如何获得HttpResponse大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆