?
Solved

HttpWebresponse

Posted on 2005-03-28
2
Medium Priority
?
255 Views
Last Modified: 2010-04-16
How to write a Stream received from an httpWebResponse into a byte array in C#?
Thanks in advance..
0
Comment
Question by:Nikhil Gupta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 14

Expert Comment

by:Thandava Vallepalli
ID: 13650204
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
 
LVL 14

Accepted Solution

by:
Thandava Vallepalli earned 375 total points
ID: 13650527
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.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

777 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