?
Solved

need help converting stream to byte array.

Posted on 2005-04-14
3
Medium Priority
?
3,759 Views
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.
0
Comment
Question by:Viraj_Kanwade
3 Comments
 
LVL 3

Expert Comment

by:wishguitar
ID: 13783355
MemoryStream ReceiveStream = (MemoryStream)myWebResponse.GetResponseStream();
Byte [] bytes = ReceiveStream.ToArray();
0
 
LVL 2

Author Comment

by:Viraj_Kanwade
ID: 13783394
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
0
 
LVL 7

Accepted Solution

by:
Ceiled earned 200 total points
ID: 13784330
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)
            {
                  byteList.Add(bytes);
                  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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

850 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