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

I am looking for answers to such questions:

  • For any given query, do these three properties always return the same value?
  • Do any of them have any side effects?
  • Does any of them block until the entire request is received by IIS?
  • Do any of them force downloaded files to fully load into memory?

This is interesting to me because I make my web application by e-mail when the request takes too much time to process on the server, and I want to avoid sending this letter for large requests, that is, when the user uploads one or more large files.

+4
source share
1 answer

According to MSDN:

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

So the last two are the same. Here's what Reflector says about the 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; } } 
+2
source

Source: https://habr.com/ru/post/1303851/


All Articles