c# - How to use WebResponse to Download .wmv file -
I am using the following code to capture a wmv file through a WebResponse. I am using a thread to call this function:
Cast object on static zero GetPage (object data) {// ThreadInfo ThreadInfo ti = (ThreadInfo) data ; // Request WebResponse wr = WebRequest.Create (ti.url) .GetResponse (); // Display value for content-length header console. WrightLine (ti.url + ":" + wr.Headers ["content-length"]); String to twenty-two = @ "C: \ Users \ Kevin \ download \ temfile" + wr.ResponseUri.PathAndQuery; StreamWriter streamWriter = new streamer (toBeSaved); MemoryStream M = new memorystream (); Stream Received Stream = wr.GetResponseStream (); (StreamReader sr = new streamminder (getstream)) using {while (sr.Peek ()> = 0) {m.WriteByte (byte) sr.Read ()); } StreamWriter.Write (sr.ReadToEnd ()); Sr.Close (); Wr.Close (); } StreamWriter.Flush (); StreamWriter.Close (); // streamReader.Close (); // Parents know that the process is done ti.are.Set (); Wr.Close (); }
The file seems to download properly, but Windows Media Viewer can not open the file correctly.
Am I missing the incredibly easy thing?
You need to download it as a binary instead of a text here. Here's a way for you Should do the trick.
Public Zero DownloadFile (string url, string toLocalPath) {byte [] result = null; Byte [] buffer = new byte [40 9 7]; WebRequest wr = WebRequest.Create (url); WebResponse response = wr.GetResponse (); Stream response stream = response. GetResponseStream; Memorystream memorystream = new memorystream (); Int count = 0; Do {count = responsestream.Read (buffer, 0, buffer lang); MemoryStream.Write (buffer, 0, count); If (calculation == 0) {break; }} While (true); Results = MemoryStream. ToArray; File stream fs = new filestream (toLocalPath, FileMode.OpenOrCreate, FileAccess.ReadWrite); Fs.Write (result, 0, result. Length); Fs.Close (); MemoryStream.Close (); ResponseStream.Close (); }
Comments
Post a Comment