HttpWebRequest - data is truncated?

L

Logician

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();
}


}
 
J

Jon Skeet [C# MVP]

Logician said:
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#.

The StreamReader is probably buffering data - reading more than 25
bytes and then saving it in its buffer.

Instead of using a StreamReader, just use the Stream directly to start
with. You're interested in the bytes which represent "GIF8", not the
actual *text* "GIF8", right?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top