HttpWebRequest - decode response


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()
                // 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)

                // execute the request
                HttpWebResponse response = (HttpWebResponse)

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

                string tempString = null;
                int count = 0;

                    // 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
                while (count > 0); // any more data to read?

                // print out page source
                return true;
            catch (Exception ex)
                return false;
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.


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

Open in new window

FleurysAuthor Commented:
Both methods give the same results.

Thanks much.
All Courses

From novice to tech pro — start learning today.