Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

HttpWebresponse

How to write a Stream received from an httpWebResponse into a byte array in C#?
Thanks in advance..
0
Nikhil Gupta
Asked:
Nikhil Gupta
  • 2
1 Solution
 
Thandava VallepalliCommented:
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

// we will read data via the response stream
Stream resStream = response.GetResponseStream();
            
    int Length = 256;
    Byte [] buffer = new Byte[Length];
    int bytesRead = readStream.Read(buffer,0,Length);


itsvtk

0
 
Thandava VallepalliCommented:
using System;
using System.IO;
using System.Net;
using System.Text;


/// <summary>
/// Fetches a Web Page
/// </summary>
class WebFetch
{
      static void Main(string[] args)
      {
            // used to build entire input
            StringBuilder sb  = new StringBuilder();

            // used on each read operation
            byte[]        buf = new byte[8192];

            // prepare the web page we will be asking for
            //HttpWebRequest  request  = (HttpWebRequest)      WebRequest.Create("http://10.0.0.55/aspnet/");
            HttpWebRequest  request  = (HttpWebRequest)      WebRequest.Create("https://secure.experts-exchange.com/jsp/editPremiumServices.jsp");

            // execute the request
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            // we will read data via the response stream
            Stream resStream = response.GetResponseStream();

            string tempString = null;
            int    count      = 0;

            do
            {
                  // fill the buffer with data
                  count = resStream.Read(buf, 0, buf.Length);

                  // make sure we read some data
                  if (count != 0)
                  {
                        // translate from bytes to ASCII text
                        tempString = Encoding.ASCII.GetString(buf, 0, count);

                        // continue building the string
                        sb.Append(tempString);
                  }
            }
            while (count > 0); // any more data to read?
            resStream.Close();
            
            
            // print out page source
            Console.WriteLine(sb.ToString());
            
            
            //converting stringbuilder object to byte array object
              char[] chars = sb.ToString().ToCharArray();
            
              Encoder uniEncoder = Encoding.Unicode.GetEncoder();
       
              int byteCount = uniEncoder.GetByteCount(chars, 0, chars.Length, true);
              Byte[] bytes = new Byte[byteCount];
              int bytesEncodedCount = uniEncoder.GetBytes(chars, 0, chars.Length, bytes, 0, true);
            
            Console.Write("Encoded bytes: ");
              foreach (Byte b in bytes)
            {
            Console.Write("[{0}]", b);
              }
              Console.WriteLine();
      }
}



itsvtk
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now