/////
///// Http方式下载文件
/////
///// http地址
///// 本地文件
/////
//public static void HttpDownload(HttpContext context)
//{
// string pathUrl = context.Request[“url”];
// 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 = “C://Windows//Temp”;
// // 如果不存在就创建file文件夹
// if (!Directory.Exists(path))
// {
// if (path != null) Directory.CreateDirectory(path);
// }
// path = path + “//”+context.Request[“local”]+"";
// 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();
//}
使用C#实现HTTP文件下载
该博客介绍了如何使用C#进行HTTP文件下载操作。通过创建HttpWebRequest对象,设置超时时间并获取HttpWebResponse,然后将远程文件保存到本地指定路径,确保文件夹存在并逐块读写数据。
3360

被折叠的 条评论
为什么被折叠?



