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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FleurysAuthor Commented:
Both methods give the same results.

Thanks much.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.