need help converting stream to byte array.

Posted on 2005-04-14
Last Modified: 2009-05-19
Stream ReceiveStream = myWebResponse.GetResponseStream();

i am using this stream and need to convert it to a byte array. i have to pass the byte array to another function ao i need the stream converted whole to byte array and the size of data received in stream is variable.
so can any1 please give me a code to convert this.
Question by:Viraj_Kanwade
    LVL 3

    Expert Comment

    MemoryStream ReceiveStream = (MemoryStream)myWebResponse.GetResponseStream();
    Byte [] bytes = ReceiveStream.ToArray();
    LVL 2

    Author Comment

    MemoryStream ReceiveStream = (MemoryStream)myWebResponse.GetResponseStream();

    on this i get a >> specified cast is not valid << exception
    is there any other way or some changes needed to the above code
    LVL 7

    Accepted Solution

    The cast fails because WebResponse doesn't return a MemoryStream -- for an HTTP response, it actually returns a System.Net.ConnectStream, which is internal to System.dll. You don't want to rely on that. All you need to care about is that it's a Stream.

          using (Stream s = myWebResponse.GetResponseStream())
                // Since we don't know how many bytes there will be,
                // use a dynamic list to store them
                ArrayList byteList = new ArrayList();

                BinaryReader reader = new BinaryReader(s);
                int totalLength = 0;
                byte[] bytes;

                // Keep reading until the reader returns 0 bytes
                while ((bytes = reader.ReadBytes(1024)).Length > 0)
                      totalLength += bytes.Length;

                // Create a byte array to store the final result
                bytes = new byte[totalLength];
                int position = 0;

                // Cycle through the list of byte arrays we read in
                // and copy each in turn to the final result
                foreach (byte[] b in byteList)
                      Array.Copy(b, 0, bytes, position, b.Length);
                      position += b.Length;

    At this point, "bytes" contains the bytes you want. A simpler (code-wise) solution would be to create a StreamReader instead of a BinaryReader, use ReadToEnd() to grab the entire string, then use an Encoding object to get the bytes back, but this requires knowing the encoding of the source stream and the encoding that the code you pass the array to wants, so it's not quite as robust. Unfortunately, I couldn't find a binary equivalent of TextReader.ReadToEnd(), but the code above is basically what it would look like.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now