0 out of 2 rated this helpful - Rate this topic

HttpRequest.InputStream Property

Gets the contents of the incoming HTTP entity body.

Namespace:  System.Web
Assembly:  System.Web (in System.Web.dll)
public Stream InputStream { get; }

Property Value

Type: System.IO.Stream
A Stream object representing the contents of the incoming HTTP content body.

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();            
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

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.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
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);
Dim to length-1 ?
Shouldn't the byte array be dimensioned to length-1, since the index starts at 0?
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.