I am looking for some help about the method below which partly works.
The problem is that the data returned is truncated in that the
returned data has missing bytes near the beginning of the data. I am
using VS 2005 and C#.
Has anyone got any ideas?
public string getWebPage(string url)
{
// add error handling
StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de-
DE;"+
"rv:1.7.5) Gecko/20041108 Firefox/1.0";
request.Timeout=20000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader myTextReader = new StreamReader(responseStream);
char[] strBuffer = new char[25];
myTextReader.ReadBlock(strBuffer,0,25);
string stringBuffer = new string(strBuffer);
if (stringBuffer.IndexOf("GIF8")>-1 ||
stringBuffer.IndexOf("JFIF")>-1)
{
// image found
return "image";
}
else
{
// this is a text page
}
string tempString = null;
int count = 0;
do
{
count = responseStream.Read(buf,0,buf.Length);
if (count!=0)
{
tempString=Encoding.ASCII.GetString(buf,0,count);
sb.Append(tempString);
}
}while (count>0);
}
catch (WebException e)
{
return "ZenoBot getWebPage web failed to fetch "+e.ToString();;
}
catch (Exception e)
{
return "ZenoBot getWebPage web failed to fetch "+e.ToString();;
}
return sb.ToString();
}
}