日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 实现将网络资源保存到本地

發布時間:2023/12/10 C# 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 实现将网络资源保存到本地 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/// <summary>/// 單個文件保存從對方服務器到自己網站/// </summary>/// <param name="aUrl"></param>/// <param name="aPath"></param>/// <returns></returns>[HttpGet]public string FileSave(string aUrl,string aPath) {HttpWebRequest request = (HttpWebRequest)WebRequest.Create(aUrl);request.Method = "GET";using (HttpWebResponse response = request.GetResponse() as HttpWebResponse){if (response.StatusCode == HttpStatusCode.OK){Stream rs = response.GetResponseStream();//網絡流轉換為內存流var ms = StreamToMemoryStream(rs);ms.Seek(0, SeekOrigin.Begin); int buffsize = (int)ms.Length;byte[] bytes = new byte[buffsize];ms.Read(bytes, 0, buffsize);string bPath = Server.MapPath(@"~/Upload/") + aPath; //winform可寫物理磁盤路徑d:/123.rarstring bPathS = System.IO.Path.GetDirectoryName(bPath);if (!Directory.Exists(bPathS))Directory.CreateDirectory(bPathS);FileStream fs = new FileStream(bPath, FileMode.Create, FileAccess.Write);fs.Write(bytes, 0, bytes.Length);fs.Flush();fs.Close();ms.Flush(); ms.Close();rs.Flush(); rs.Close();//以文件流的方式下載}}return "ok";}/// <summary>/// 單個文件保存從對方服務器到自己網站/// </summary>/// <param name="aUrl">對方服務器請求路徑</param>/// <param name="aPath">本網站存儲路徑</param>/// <returns></returns>[HttpGet]public string FileSave(string aUrl, string aPath){System.GC.Collect();System.Net.ServicePointManager.DefaultConnectionLimit = 512;HttpWebRequest request = null;try{request = (HttpWebRequest)WebRequest.Create(aUrl);request.Method = "GET";request.KeepAlive = false;request.Proxy = WebRequest.DefaultWebProxy;using (HttpWebResponse response = request.GetResponse() as HttpWebResponse){if (response.StatusCode == HttpStatusCode.OK && response.ContentLength > 1000){Stream rs = response.GetResponseStream();//網絡流轉換為內存流var ms = StreamToMemoryStream(rs);ms.Seek(0, SeekOrigin.Begin);int buffsize = (int)ms.Length;byte[] bytes = new byte[buffsize];ms.Read(bytes, 0, buffsize);string bPath = Server.MapPath(@"~") + aPath;string bPathS = System.IO.Path.GetDirectoryName(bPath);if (!Directory.Exists(bPathS))Directory.CreateDirectory(bPathS);FileStream fs = new FileStream(bPath, FileMode.Create, FileAccess.Write);fs.Write(bytes, 0, bytes.Length);fs.Flush();fs.Close();ms.Flush(); ms.Close();rs.Flush(); rs.Close();//以文件流的方式下載}else{return "fail";}}return "ok";}catch (Exception e){return "error";}finally{if (request != null)request.Abort();//關閉連接}}private static MemoryStream StreamToMemoryStream(Stream instream){MemoryStream outstream = new MemoryStream();const int bufferLen = 4096;byte[] buffer = new byte[bufferLen];int count = 0;while ((count = instream.Read(buffer, 0, bufferLen)) > 0){outstream.Write(buffer, 0, count);}return outstream;}

總結

以上是生活随笔為你收集整理的C# 实现将网络资源保存到本地的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。