HTTP下载文件到本地

///// <summary>
///// Http方式下载文件
///// </summary>
///// <param name="url">http地址</param>
///// <param name="localfile">本地文件</param>
///// <returns></returns>
//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();
//}

posted @ 2020-12-23 17:06  CC小骨  阅读(880)  评论(0编辑  收藏  举报