利用backgroundwork----递归读取网页源代码,并下载href链接中的文件
今天闲着没事,研究了一下在线更新程序版本的问题。也是工作中的需要,开始不知道如何下手,各种百度也没有找到自己想要的,因为我的需求比较简单,所以就自己琢磨了一下。讲讲我的需求吧。自己在IIs上发布了一个网站,这个网站仅仅只是内部使用的,网站的内容就是我的另外一个程序(就叫A程序吧)的打包发布的文件放进去。然后在客户端启动我的A程序之前检查是否有新版本文件发布。如果有,我根据网页源代码的信息和本地文件信息进行比较,决定是否下载。如果有下载,下载完成后执行A程序的.exe文件启动A程序。大致的要求就是这样。
首先自己发布一个测试网站,也就是简单的在IIS上将我本机的一个文件夹发布出来,具体怎么操作就不做讲解了。得到我的网址:http://localhost/webTest/。这个网站就作为我以后有新版本文件要发布就直接丢进去。
上面的截图中有几个地方需要注明一下:
1.是这个文件最后一次编辑日期。
2.是最后一次编辑时间点。
3.是你这个文件的大小。
4.椭圆部分是一个文件夹。
前面标题说用递归,就是因为网站中可能存在子文件夹,遇到子文件夹我就要继续跟进去读取源代码获取我要的信息。
注:网页中有个[to parent Directory]这是他的父文件夹,我们在读取网页源代码的时候要对这部分进行处理
注:1,2部分是指这个文件最后一次编辑时间,比如说你在本地有个文件你对他进行最后一次的编辑时间2016/8/26 13:15 那不管你把这个文件拷贝或是上传到其他地方,那他的编辑时间始终不会变的。
大致的情况介绍的差不多了,接下来直接开始我的读取网页下载文件的程序吧!上代码,一如既往,图文并茂的文章才是好文章。
一、创建一个winform工程。
图(1):工程结构
图(2):winform需要的控件
图(1)中我添加了两个帮助类:FileHelper.cs/HttpHelper.cs。在后面做详细介绍
图(2)中1是一个label控件,用来显示正在下载的文件名。2是progressBar控件,winform自带的进度条控件,我觉得还挺好用的。。还需要一个backgroundwork控件
二:帮助类文件
FileHelper.cs帮助类文件。
1 public class FileHelper 2 { 3 public DateTime ModiDate { get; set; } //最后编辑时间 4 5 public long Size { get; set; } //文件大小 6 7 public String FilePath { get; set; } //路径+文件名 8 }
HttpHelper.cs
1 /// <summary> 2 /// 获取网页源代码 3 /// </summary> 4 /// <param name="serverUrl">网址</param> 5 /// <param name="listFile">存放下载文件的集合</param> 6 /// <param name="listHref">存放子目录集合</param> 7 8 public static void GetHtmlResource(string serverUrl, List<FileHelper> listFile, List<string> listHref) 9 { 10 #region 11 //Uri u = new Uri(serverUrl); 12 //string host = u.Host; 13 //if (serverUrl.EndsWith("/")) 14 //{ 15 // //1.获取网页源代码 16 // WebClient wc = new WebClient(); 17 // wc.Credentials = CredentialCache.DefaultCredentials; 18 // byte[] htmlData = wc.DownloadData(serverUrl); 19 // string htmlStr = Encoding.Default.GetString(htmlData); 20 // //2.正则找到href属性内容截取 21 // string regMat = @"(?is)<a[^>]*?href=(['""s]?)(?<href>[^'""s]*)1[^>]*?"; 22 // MatchCollection mat = Regex.Matches(htmlStr, regMat, RegexOptions.IgnoreCase); 23 // List<string> listHref = new List<string>(); //存放href结合 24 // for (int i = 0; i < mat.Count; i++) 25 // { 26 // string item = mat[i].Groups["href"].Value; 27 // listHref.Add(item); 28 // MatchCollection match = Regex.Matches(htmlStr, "([0-9]{1,})\s\<A\sHREF=""+ item+""", RegexOptions.IgnoreCase); 29 // if(match.Count == 1 && match[0].Groups.Count==2) 30 // { 31 // fileSize.Add(@"http://" + host + item, int.Parse(match[0].Groups[1].Value)); 32 // } 33 // } 34 // foreach (var item in listHref) //Match item in mat 35 // { 36 // string url = @"http://"+host + item; 37 // if (serverUrl.StartsWith(url)) 38 // { 39 // continue; 40 // } 41 // GetHtmlResource(url, serverFilePath,fileSize); 42 // } 43 //} 44 //else 45 //{ 46 // serverFilePath.Add(serverUrl); 47 //} 48 #endregion 49 50 Uri u = new Uri(serverUrl); 51 string host = u.Host; 52 if (serverUrl.EndsWith("/")) 53 { 54 //1.获取网页源代码 55 WebClient wc = new WebClient(); 56 wc.Credentials = CredentialCache.DefaultCredentials; 57 byte[] htmlData = wc.DownloadData(serverUrl); 58 string htmlTempStr = Encoding.Default.GetString(htmlData); 59 //完全用字符串截取的方式得到自己想要的东西 60 htmlTempStr = htmlTempStr.Substring(htmlTempStr.IndexOf("<pre>")); 61 htmlTempStr = htmlTempStr.Substring(0, htmlTempStr.IndexOf("</pre>")); 62 htmlTempStr = htmlTempStr.Replace("<pre>", ""); 63 htmlTempStr = htmlTempStr.Replace("</pre>", ""); 64 htmlTempStr = htmlTempStr.Replace("<dir>", "-1"); //把子菜单前面的"<dir&"改为-1,为了跟其他的信息一致有规律 65 htmlTempStr = htmlTempStr.Replace("<br>", "#"); 66 string[] tempStr = htmlTempStr.Split('#'); 67 ArrayList listStr = new ArrayList(tempStr); 68 //移除每个新网页的父级文件夹 69 listStr.RemoveAt(0); 70 for (int i = 0; i < listStr.Count; i++) 71 { 72 if (String.IsNullOrWhiteSpace(listStr[i].ToString())) 73 { 74 listStr.RemoveAt(i); 75 } 76 } 77 tempStr = (string[])listStr.ToArray(typeof(string)); 78 79 for (int f = 0; f < tempStr.Length; f++) 80 { 81 //截取最后修改日期带时间 82 string fileModiTime = tempStr[f].Substring(0, 20); 83 //截取文件大小 84 string fileSize = tempStr[f].Substring(20, tempStr[f].IndexOf("<A") - 20); 85 //截取文件路径 86 string filePath = tempStr[f].Split('"')[1]; 87 FileHelper file = new FileHelper(); 88 file.ModiDate = Convert.ToDateTime(fileModiTime.Trim()); 89 file.Size = Convert.ToInt32(fileSize.Trim()); 90 file.FilePath = @"http://" + host + filePath; 91 //如果大小为-1,我就认为是子文件夹,添加到集合中 92 if (file.Size == -1) 93 { 94 listHref.Add(file.FilePath); 95 } 96 else 97 { 98 //添加到要下载的文件集合中 99 listFile.Add(file); 100 } 101 } 102 //循环我的子文件夹集合 103 foreach (var item in listHref) 104 { 105 //如果item等于我的serverUrl继续 106 if (serverUrl.StartsWith(item)) 107 { 108 continue; 109 } 110 //递归 111 GetHtmlResource(item, listFile, listHref); 112 } 113 114 } 115 }