How to use WebResponse to Download .wmv file

拜拜、爱过 提交于 2019-12-04 07:28:51

You just need to download it as binary instead of text. Here's a method that should do the trick for you.

public void DownloadFile(string url, string toLocalPath)
{
    byte[] result = null;
    byte[] buffer = new byte[4097];

    WebRequest wr = WebRequest.Create(url);

    WebResponse response = wr.GetResponse();
    Stream responseStream = response.GetResponseStream;
    MemoryStream memoryStream = new MemoryStream();

    int count = 0;

    do {
        count = responseStream.Read(buffer, 0, buffer.Length);
        memoryStream.Write(buffer, 0, count);

        if (count == 0) {
            break;
        }
    }
    while (true);

    result = memoryStream.ToArray;

    FileStream fs = new FileStream(toLocalPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);

    fs.Write(result, 0, result.Length);

    fs.Close();
    memoryStream.Close();
    responseStream.Close();
}

I do not understand why you are filling MemoryStream m one byte at a time, but then writing the sr to the file. At that point, I believe the sr is empty, and MemoryStream m is never used.

Below is some code I wrote to do a similar task. It gets a WebResponse in 32K chunks at a time, and dumps it directly to a file.

public void GetStream()
{
    // ASSUME: String URL is set to a valid URL.
    // ASSUME: String Storage is set to valid filename.

    Stream response = WebRequest.Create(URL).GetResponse().GetResponseStream();
    using (FileStream fs = File.Create(Storage)) 
    {
        Byte[] buffer = new Byte[32*1024];
        int read = response.Read(buffer,0,buffer.Length);
        while (read > 0)
        {
            fs.Write(buffer,0,read);
            read = response.Read(buffer,0,buffer.Length);
        }
    }
    // NOTE: Various Flush and Close of streams and storage not shown here.
}

You are using a StreamReader and a StreamWriter to transfer your stream, but those classes are for handling text. Your file is binary and chances are that sequences of CR, LF and CR LF may get clobbered when you transfer the data. How NUL characters are handled I have no idea.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!