I'm trying to fetch a big file from internet with C# with WebRequest
. So far all other stuff that I have is working fine, but it appears that some part in the middle of the response I get is removed. I tried printing out the response.ContentLength
and getResponseContent(response).Length
and I got -1
and 80000
respectively. Is there a limit on the WebResponse
length or it's the helper function that's buggy? How do I get the full content?
Here's my getResponseContent
function:
private static String getResponseContent(HttpWebResponse response)
{
Stream responseStream = response.GetResponseStream();
byte[] buffer = new byte[1000];
String ret = "";
while (responseStream.Read(buffer, 0, 1000) > 0)
ret += (System.Text.Encoding.Default.GetString(buffer));
return ret;
}
Thanks to @Neolisk, I've re-written my getResponseContent
to use the StreamReader
class and it works like magic. Here's the code:
private static String getResponseContent(HttpWebResponse response)
{
Stream responseStream = response.GetResponseStream();
StreamReader sr = new StreamReader(responseStream);
return sr.ReadToEnd();
}
However, still, can anyone explain why the the ContentLength in the response header is -1 instead some meaningful length?