I think this is very close to what @ Zachary offers. And this (seems) work (s); in fact, I think using using , as @Zachary does even “better.”
The main thing is that I cannot see the GetResponse() lock behavior that you (seem to be) describing.
In addition, the following code only roughly shows how everything works; he will not read the stream to the end, for example (if not a coincidence :)). But it should work if you copy-n-paste it into an empty Console Application project in Visual Studio.
You can try using some “shorter” URL for the test. In this example, the ISO of the debian distribution is downloaded (bit over 600 MB). Sorry, debian, I didn’t want to steal your bandwidth. → Btw: is there anything reasonable that can be used to test such a scenario?
The code is strongly inspired by C # - how to read a continuous stream of XML over HTTP .
namespace StreamReadWebRequest { using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO; class Program { static void Main(string[] args) { HttpWebRequest req; HttpWebResponse res = null; try { req = (HttpWebRequest)WebRequest.Create( "http://cdimage.debian.org/debian-cd/5.0.4/i386/iso-cd/debian-504-i386-CD-1.iso"); res = (HttpWebResponse)req.GetResponse(); Stream stream = res.GetResponseStream(); byte[] data = new byte[4096]; int read; while ((read = stream.Read(data, 0, data.Length)) > 0) { Process(data, read); } } finally { if (res != null) res.Close(); } Console.In.Read(); } private static void Process(byte[] data, int read) { Console.Out.Write(ASCIIEncoding.ASCII.GetString(data)); } } }
scherand
source share