c# 视频下载

string pathUrl = "";
System.Net.HttpWebRequest request = null;
System.Net.HttpWebResponse response = null;
//请求网络路径地址
request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(pathUrl);
request.Timeout = 5000; // 超时时间
//获得请求结果
response = (System.Net.HttpWebResponse)request.GetResponse();
//文件下载地址
string path = "E:/文件下载";
// 如果不存在就创建file文件夹
if (!Directory.Exists(path))
{
if (path != null) Directory.CreateDirectory(path);
}
path = path + "/20190402035457.mp4";
Stream stream = response.GetResponseStream();
//先创建文件
Stream sos = new System.IO.FileStream(path, System.IO.FileMode.Create);
byte[] img = new byte[1024];
int total = stream.Read(img, 0, img.Length);
while (total > 0)
{
//之后再输出内容
sos.Write(img, 0, total);
total = stream.Read(img, 0, img.Length);
}
stream.Close();
stream.Dispose();
sos.Close();
sos.Dispose();

猜你喜欢

转载自www.cnblogs.com/wangzhenghao/p/11235642.html
今日推荐