Link to home
Start Free TrialLog in
Avatar of Fleurys
Fleurys

asked on

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;
            }
        }
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Fleurys
Fleurys

ASKER

Both methods give the same results.

Thanks much.