HttpRequest.InputStream Property
.NET Framework 4
Gets the contents of the incoming HTTP entity body.
Assembly: System.Web (in System.Web.dll)
The following code example copies the contents of an InputStream into a string.
System.IO.Stream str; String strmContents; Int32 counter, strLen, strRead; // Create a Stream object. str = Request.InputStream; // Find number of bytes in stream. strLen = Convert.ToInt32(str.Length); // Create a byte array. byte[] strArr = new byte[strLen]; // Read stream into byte array. strRead = str.Read(strArr, 0, strLen); // Convert byte array to a text string. strmContents = ""; for (counter = 0; counter < strLen; counter++) { strmContents = strmContents + strArr[counter].ToString(); }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Inefficient sample
> Shouldn't the byte array be dimensioned to length-1, since the index starts at 0?
No, construct parameter for arrays takes size of array, not zero based upper bound. See http://msdn.microsoft.com/en-us/library/9b9dty7d(v=VS.100).aspx
But processing a string byte-by-byte is quite inefficient. In the sample provided by MS above a single byte is convertet to string (creating 1 object), then added to a string (creating 1 object and copying existing data which keeps increasing (so size to copy would be 1, 2, 3, 4, 5...100kb)). If the input data is 100 kb it would create 200.000 objects more than required and (mem)copy 4.999.950.000 bytes more than required. And then we haven't even touched the fact that the kernel operates at 4k pages.
As m. charmois stated: strmContents = System.Text.Encoding.UTF8.GetString(strArr);
No, construct parameter for arrays takes size of array, not zero based upper bound. See http://msdn.microsoft.com/en-us/library/9b9dty7d(v=VS.100).aspx
But processing a string byte-by-byte is quite inefficient. In the sample provided by MS above a single byte is convertet to string (creating 1 object), then added to a string (creating 1 object and copying existing data which keeps increasing (so size to copy would be 1, 2, 3, 4, 5...100kb)). If the input data is 100 kb it would create 200.000 objects more than required and (mem)copy 4.999.950.000 bytes more than required. And then we haven't even touched the fact that the kernel operates at 4k pages.
As m. charmois stated: strmContents = System.Text.Encoding.UTF8.GetString(strArr);
- 9/13/2011
- Tedd Hansen
- 9/13/2011
- Tedd Hansen
Dim to length-1 ?
Shouldn't the byte array be dimensioned to length-1, since the index starts at 0?
- 8/2/2011
- Doug Deal
This sample returns a table of the ASCII codes
It seems that this sample returns a table of the ASCII codes.
You should rather use this:
strmContents = System.Text.Encoding.UTF8.GetString(strArr);
That is also much shorter.
You should rather use this:
strmContents = System.Text.Encoding.UTF8.GetString(strArr);
That is also much shorter.
- 9/16/2010
- m. charmois
- 9/16/2010
- m. charmois