通过http下载音频并在c#中存储在本地文件夹中
本文关键字:存储 文件夹 http 音频 通过 下载 | 更新日期: 2023-09-27 18:07:04
谁能分享我一段代码在c#,我可以下载音频文件的。wmv格式使用http请求和存储在本地文件夹?
可以使用web客户端
using System.Net;
WebClient webClient = new WebClient();
webClient.DownloadFile("http://example.com/myfile.wmv", @"c:'myfile.wmv");
使用HTTP web请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://example.com/myfile.wmv");
request.Method = WebRequestMethods.Http.Get;
request.ContentType = "video/x-ms-wmv";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream reader = response.GetResponseStream();
byte[] inBuf = new byte[response.ContentLength];
int bytesToRead = (int)inBuf.Length;
int bytesRead = 0;
while (bytesToRead > 0)
{
int n = reader.Read(inBuf, bytesRead, bytesToRead);
if (n == 0)
break;
bytesRead += n;
bytesToRead -= n;
}
FileStream fstr = new FileStream(@"c:'myfile.wmv", FileMode.OpenOrCreate,
FileAccess.Write);
fstr.Write(inBuf, 0, bytesRead);
reader.Close();
fstr.Close();