Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Get value to byte() [modified]

Get value to byte() [modified]

Scheduled Pinned Locked Moved C#
data-structuresquestion
7 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hung_ngole
    wrote on last edited by
    #1

    Hi, I have the following code, how can I get the value of the stream into byte array (the response contains binary data and using the readToEnd() fails) HttpWebResponse response = request.GetResponse() System.IO.Stream responseStream = response.GetResponseStream() Thanks in advance, Hung. Hung -- modified at 12:02 Sunday 9th July, 2006

    E N 2 Replies Last reply
    0
    • H hung_ngole

      Hi, I have the following code, how can I get the value of the stream into byte array (the response contains binary data and using the readToEnd() fails) HttpWebResponse response = request.GetResponse() System.IO.Stream responseStream = response.GetResponseStream() Thanks in advance, Hung. Hung -- modified at 12:02 Sunday 9th July, 2006

      E Offline
      E Offline
      Eran Aharonovich
      wrote on last edited by
      #2

      You can check my article about browsing the WEB with C#: Browse the WEB with C# Eran Aharonovich (eran.aharonovich@gmail.com ) Noviway

      H 1 Reply Last reply
      0
      • H hung_ngole

        Hi, I have the following code, how can I get the value of the stream into byte array (the response contains binary data and using the readToEnd() fails) HttpWebResponse response = request.GetResponse() System.IO.Stream responseStream = response.GetResponseStream() Thanks in advance, Hung. Hung -- modified at 12:02 Sunday 9th July, 2006

        N Offline
        N Offline
        Nader Elshehabi
        wrote on last edited by
        #3

        Hello Try this:

        HttpWebResponse response = request.GetResponse()
        System.IO.Stream responseStream = response.GetResponseStream()
        byte[] input = new byte[responseStream.Length];
        responseStream.Read(input, 0, (int)responseStream.Length - 1);

        Regards:rose:

        H 1 Reply Last reply
        0
        • N Nader Elshehabi

          Hello Try this:

          HttpWebResponse response = request.GetResponse()
          System.IO.Stream responseStream = response.GetResponseStream()
          byte[] input = new byte[responseStream.Length];
          responseStream.Read(input, 0, (int)responseStream.Length - 1);

          Regards:rose:

          H Offline
          H Offline
          hung_ngole
          wrote on last edited by
          #4

          Although the stream has data, the length of responseStream is -1. This is the main problem I facing !! Thanks anyway! Hung. Hung

          S 1 Reply Last reply
          0
          • E Eran Aharonovich

            You can check my article about browsing the WEB with C#: Browse the WEB with C# Eran Aharonovich (eran.aharonovich@gmail.com ) Noviway

            H Offline
            H Offline
            hung_ngole
            wrote on last edited by
            #5

            I have the problem with binary data (Ex: image) not the textual data! Thanks anyway, Hung. Hung

            E 1 Reply Last reply
            0
            • H hung_ngole

              Although the stream has data, the length of responseStream is -1. This is the main problem I facing !! Thanks anyway! Hung. Hung

              S Offline
              S Offline
              S Senthil Kumar
              wrote on last edited by
              #6

              According to the MSDN[^] documentation, Read returns the number of bytes actually read. So you could always create a buffer of some arbitrary size, say 512 bytes, and then loop till Read returns less than 512 bytes. Something like.

              ArrayList finalBuffer = new ArrayList();
              byte []buffer = new byte[512];

              int bytesRead = 0;
              while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
              {
                 for (int i = 0; i<bytesRead; ++i)
                 {
                     finalBuffer.Add(buffer[i]);
                 }
              }

              Regards Senthil _____________________________ My Blog | My Articles | WinMacro

              1 Reply Last reply
              0
              • H hung_ngole

                I have the problem with binary data (Ex: image) not the textual data! Thanks anyway, Hung. Hung

                E Offline
                E Offline
                Eran Aharonovich
                wrote on last edited by
                #7

                You are right. Sorry. This is what you need to do: // Prepare web request... HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.noviway.com"); // We use POST ( we can also use GET ) myRequest.Method = "GET"; // Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable. HttpWebResponse myHttpWebResponse= (HttpWebResponse)myRequest.GetResponse(); // Display the contents of the page to the console. Stream streamResponse = myHttpWebResponse.GetResponseStream(); MemoryStream memStream = new MemoryStream(); byte[] readBuffer = new byte[256]; // Read from buffer int count = streamResponse.Read( readBuffer, 0, 256 ); while (count > 0) { memStream.Write(readBuffer, 0, count); // Read from buffer count = streamResponse.Read( readBuffer, 0, 256); } byte[] originalBuffer = memStream.ToArray(); // Release the response object resources. streamResponse.Close(); // Close response myHttpWebResponse.Close(); Eran Aharonovich (eran.aharonovich@gmail.com ) Noviway

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups