Click to See Complete Forum and Search --> : Response.ContentLength = -1


burnt1ce85
04-10-2007, 04:19 PM
Hey guys.

I'm trying to write a c# program that downloads a xml file, and reads it into an array of bytes.

My problem is before i can get the BinaryReader to read the xml file, i have to specify how many bytes to read. Usually, i would use Response.ContentLength but in some cases it's equal to -1. According to google searches, ContentLength is optional and in this case, i have to calculate it myself.

How do i calculate ContentLength?

Thanks

PeOfEo
04-11-2007, 09:23 AM
I believe you could use PeekChar(), read in a while loop while PeekChar() > -1, if you need to calculate the content length for some specific reason other than reading you could just do the same operation above, but increment a counting variable.

Cstick
04-11-2007, 09:25 PM
Try something like this;


WebRequest myWebRequest = WebRequest.Create("http://www.webdeveloper.com");
WebResponse myWebResponse = myWebRequest.GetResponse();

Stream ReceiveStream = myWebResponse.GetResponseStream();
BinaryReader readStream = new BinaryReader(ReceiveStream);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
byte[] read = new byte[256];

int count = readStream.Read(read, 0, 256);
while (count > 0)
{
ms.Write(read, 0, count);
count = readStream.Read(read, 0, 256);
}

byte[] output = ms.ToArray();

readStream.Close();
myWebResponse.Close();

Response.BinaryWrite(output);
Response.End();

burnt1ce85
04-18-2007, 03:20 PM
Thanks for your help Cstick! I appreciate it alot!