using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Data; using System.Text; using System.Threading; using System.Threading.Tasks; using RestSharp; using RestSharp.Extensions; using RestSharp.Authenticators; using RestSharp.Deserializers; using RestSharp.Validation; using Geovin.Du.Commn; /// <summary> /// 涂聚文 Geovin Du geovindu /// /// </summary> public partial class DownloadImge : System.Web.UI.Page { /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button1_Click(object sender, EventArgs e) { string weburl = this.txtURL.Text.Trim(); int stat = int.Parse(this.txtStart.Text.Trim()); int end = int.Parse(this.txtEnd.Text.Trim())+1; int k = 0; int er = 0; for (int i = stat; i< end; i++) { string fileurl = HttpContext.Current.Server.MapPath(@"ImgFile//" + i.ToString() +this.txtExift.Text.Trim()); DownloadData(weburl+i.ToString()+this.txtExift.Text.Trim(), fileurl); k++; } Response.Write("Ok" + k.ToString() + ",erro" + er.ToString()); } /// <summary> /// 文件下载 /// </summary> /// <param name="website"></param> /// <param name="file"></param> private async void RunBackground(string website, string file) { int k = 1; int er = 1; var succeed = await DownLoadGlobalHistoryNew(website, file); if (succeed) { k++; } else { er++; } } /// <summary> /// /// </summary> /// <param name="errfile"></param> /// <returns></returns> string geterro(List<string> errfile) { string str = string.Empty; if (errfile.Count > 0) { int k = 0; foreach (string s in errfile) { if (k == 0) { str = s; } else { str = str + "," + s; } k++; } } return str; } /// <summary> /// /// </summary> /// <param name="url"></param> /// <param name="file"></param> /// <returns></returns> public Task<bool> DownLoadGlobalHistoryNew(string url, string file) { string dataString = string.Empty; return Task.Run(() => { var succeed = false; try { var dataStringNew = new WebClient { Encoding = Encoding.UTF8 }.DownloadString(url); if (dataString != dataStringNew) { dataString = dataStringNew; Write(file, dataString); succeed = true; } } catch (Exception) { } return succeed; }); } /// <summary> /// /// </summary> /// <param name="file"></param> /// <param name="dataString"></param> private void Write(string file, string dataString) { try { if (File.Exists(file) && !string.IsNullOrWhiteSpace(dataString)) File.Delete(file); File.WriteAllText(file, dataString); } catch (Exception) { } } /// <summary> /// 图片下载 /// </summary> /// <param name="strFileUrlToDownload"></param> /// <param name="strfile"></param> protected void DownloadData(string strFileUrlToDownload, string strfile) { WebClient client = new WebClient(); byte[] myDataBuffer = client.DownloadData(strFileUrlToDownload); MemoryStream storeStream = new MemoryStream(); storeStream.SetLength(myDataBuffer.Length); storeStream.Write(myDataBuffer, 0, (int)storeStream.Length); storeStream.Flush(); string currentpath = strfile;// System.IO.Directory.GetCurrentDirectory() + @"\Folder"; using (FileStream file = new FileStream(currentpath, FileMode.Create, System.IO.FileAccess.ReadWrite)) { byte[] bytes = new byte[storeStream.Length]; storeStream.Read(bytes, 0, (int)storeStream.Length); file.Write(myDataBuffer, 0, (int)storeStream.Length); storeStream.Close(); } //The below Getstring method to get data in raw format and manipulate it as per requirement string download = Encoding.ASCII.GetString(myDataBuffer); } }