HttpWebRequest - decode response

Hi,

I've been using a routine which I obatined on the internet to capture some web pages. It all works fine but the text returned seems to be encoded.  I would appreciate it if someone could advise me on either decoding my returned string or better yet how to avoid receiving encoded text.

Thanks in Advance.


My code is as follows:
        public Boolean WebFetch()
        {
            try
            {
                // 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://www.mayosoftware.com");

                // 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?

                // print out page source
                //Console.WriteLine(sb.ToString());
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
FleurysAsked:
Who is Participating?
 
Bob LearnedConnect With a Mentor Commented:
That web page is XHTML that doesn't have any special encoding.  I would try using a StreamReader, and using reader.ReadToEnd(), instead of ASCII encoding.  The encoding would be UTF-8 in this case.


Example:

using (StreamReader reader = new StreamReader(stream))
{
   string text = reader.ReadToEnd();
}

Open in new window

0
 
FleurysAuthor Commented:
Both methods give the same results.

Thanks much.
0
All Courses

From novice to tech pro — start learning today.