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(b uf, 0, count);
// continue building the string
sb.Append(tempString);
}
}
while (count > 0); // any more data to read?
// print out page source
//Console.WriteLine(sb.ToS tring());
return true;
}
catch (Exception ex)
{
return false;
}
}
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(b
// continue building the string
sb.Append(tempString);
}
}
while (count > 0); // any more data to read?
// print out page source
//Console.WriteLine(sb.ToS
return true;
}
catch (Exception ex)
{
return false;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks much.