开发者

System.Web.HttpRequest - What is the difference between ContentLength, TotalBytes, and InputStream.Length?

开发者 https://www.devze.com 2022-12-23 03:12 出处:网络
I\'m looking for answers to questions like these: For any given request, do these three properties always return the same value?

I'm looking for answers to questions like these:

  • For any given request, do these three properties always return the same value?
  • Do any of them have side effects?
  • Do any of them block until the entire request is received by IIS?
  • Do any of them cause uploaded files to be loaded completely into memory?

I care about this because I'm making my web application email me when a request takes too 开发者_如何学运维long to process on the server, and I want to avoid sending this email for large requests, i.e. when the user uploads one or more large files.


According to MSDN:

ContentLength - specifies the length, in bytes, of content sent by the client.
TotalBytes - the number of bytes in the current input stream.
InputStream.Length - length of bytes in the input stream.

So last two are the same. Here is what Reflector says about ContentLength property:

public int ContentLength
{
    get
    {
        if ((this._contentLength == -1) && (this._wr != null))
        {
            string knownRequestHeader = this._wr.GetKnownRequestHeader(11);
            if (knownRequestHeader != null)
            {
                try
                {
                    this._contentLength = int.Parse(knownRequestHeader, CultureInfo.InvariantCulture);
                }
                catch
                {
                }
            }
            else if (this._wr.IsEntireEntityBodyIsPreloaded())
            {
                byte[] preloadedEntityBody = this._wr.GetPreloadedEntityBody();
                if (preloadedEntityBody != null)
                {
                    this._contentLength = preloadedEntityBody.Length;
                }
            }
        }
        if (this._contentLength < 0)
        {
            return 0;
        }
        return this._contentLength;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消