/// <summary> /// 从一个CHttpWebResponse实例保存文件到本地 /// </summary> /// <param name="cResponse">一个CHttpWebResponse实例</param> /// <param name="dirPath">文件夹的绝对路径</param> /// <param name="fileName">文件名,如果为null或者String.Empty则自动获取(推荐设置设置为自动获取)</param> /// <returns>如果保存成功,返回文件绝对路径,否则返回null</returns> internal static string SaveFile(HttpClient httpClient, string dirPath, string fileName) { string filePath; //如果成功接收服务器输出的文件流 //如果没有指定文件名,则自动获取文件名 if (string.IsNullOrEmpty(fileName)) { fileName = httpClient.GetFileName(); } filePath = dirPath + "\\" + fileName; //filePath = Regex.Replace(filePath, @"\{2,}", @"\"); filePath = Path.GetFullPath(filePath); return(FileUtility.SaveFile(filePath, httpClient.MemoryStream)); }
/// <summary> /// 从一个CHttpWebResponse实例保存文件到本地 /// </summary> /// <param name="cResponse">一个CHttpWebResponse实例</param> /// <param name="dirPath">文件夹的绝对路径</param> /// <param name="fileName">文件名,如果为null或者String.Empty则自动获取(推荐设置设置为自动获取)</param> /// <returns>如果保存成功,返回文件绝对路径,否则返回null</returns> internal static string SaveFile(HttpClient httpClient, string dirPath, string fileName) { string filePath; //如果成功接收服务器输出的文件流 //如果没有指定文件名,则自动获取文件名 if (string.IsNullOrEmpty(fileName)) { fileName = httpClient.GetFileName(); } filePath = dirPath + "\\" + fileName; //filePath = Regex.Replace(filePath, @"\{2,}", @"\"); filePath = Path.GetFullPath(filePath); return FileUtility.SaveFile(filePath, httpClient.MemoryStream); }
/// <summary> ///使用同步方式下载,将html内容保存为html文件,并将html内容中引用的图片,css,js,flash都保存到本地,然后将html内容中引用的地址都转换为相对地址 /// </summary> /// <param name="fileName">保存的文件名</param> /// <param name="ignoreScript">保存的时候是否忽略Script标签,若为true,则忽略JS</param> /// <param name="dirConfig">页面下载文件夹相关配置信息</param> /// <returns>如果下载成功,返回文件的本地路径,否则返回null</returns> public string SaveHtmlAndResource(string fileName, bool ignoreScript, DirConfig dirConfig) { string htmlContent = this.html; if (!dirConfig.CheckLegal()) { throw new Exception("路径配置不在统一磁盘下"); } dirConfig.CreateDir(); if (string.IsNullOrEmpty(this.url)) { throw new Exception("未指定当前url"); } //是否忽略JS if (ignoreScript) { htmlContent = this.FilterScript(); } #region 保存所有css和css中引用的图片 htmlContent = RegexLibrary.RegCssLink.Replace(htmlContent, delegate(Match match) { //由于css文件里有可能引用图片,所以在此处需要自定义css文件下载函数,将css文件中的图片下载到本地然后替换css文件中的引用路径 return(this.MatchUrl(match, dirConfig, this.url, dirConfig.HtmlDirPath, dirConfig.CssDirPath, "src", null, delegate(string cssUrl) { //css内容、css保存路径 string cssContent, cssSavePath = cssUrl; //请求css文件 //cRequest = Spider.CreateRequest(cssUrl); //cRequest.SetHeader("Referer", this.Url); //cResponse = Spider.Get(cRequest); HttpClient hc = new HttpClient(cssUrl); string content = hc.Request(); //如果请求成功 if (!string.IsNullOrEmpty(content)) { string tempFileName = hc.GetFileName(); //确定css文件保存的绝对路径 cssSavePath = dirConfig.UseWebSite ? Path.GetFullPath(PathUtility.GetSaveDir(this.Url, cssUrl, dirConfig.HtmlDirPath) + tempFileName) : Path.GetFullPath(dirConfig.CssDirPath + "\\" + tempFileName); //获取css内容 cssContent = content;// cResponse.GetContent(null, false); //下载css里引用的图片,并替换css内容中图片地址的引用 cssContent = this.ReplaceBackgroundUrl(cssContent, cssUrl, dirConfig, Path.GetDirectoryName(cssSavePath), dirConfig.ImgDirPath, false, null); //将css保存到本地 cssSavePath = FileUtility.SaveText(cssSavePath, cssContent, hc.Encoding); } //返回保存以后的css本地路径 return cssSavePath; })); }); #endregion #region 保存所有JS htmlContent = RegexLibrary.RegScriptLink.Replace(htmlContent, delegate(Match match) { return(this.MatchUrl(match, dirConfig, this.url, dirConfig.HtmlDirPath, dirConfig.JsDirPath, "src", null, null)); }); #endregion #region 保存所有图片 htmlContent = RegexLibrary.RegImg.Replace(htmlContent, delegate(Match match) { return(this.MatchUrl(match, dirConfig, this.url, dirConfig.HtmlDirPath, dirConfig.ImgDirPath, "src", null, null)); }); #endregion #region 保存所有Flash htmlContent = RegexLibrary.RegFlash.Replace(htmlContent, delegate(Match match) { return(this.MatchUrl(match, dirConfig, this.url, dirConfig.HtmlDirPath, dirConfig.FlashDirPath, "src", null, null)); }); #endregion #region 当前页面内嵌css中的图片 htmlContent = this.ReplaceBackgroundUrl(htmlContent, this.Url, dirConfig, dirConfig.HtmlDirPath, dirConfig.ImgDirPath, false, null); #endregion return(FileUtility.SaveText(Path.GetFullPath(dirConfig.HtmlDirPath + "\\" + fileName), htmlContent, this.encode)); }
/// <summary> ///使用同步方式下载,将html内容保存为html文件,并将html内容中引用的图片,css,js,flash都保存到本地,然后将html内容中引用的地址都转换为相对地址 /// </summary> /// <param name="fileName">保存的文件名</param> /// <param name="ignoreScript">保存的时候是否忽略Script标签,若为true,则忽略JS</param> /// <param name="dirConfig">页面下载文件夹相关配置信息</param> /// <returns>如果下载成功,返回文件的本地路径,否则返回null</returns> public string SaveHtmlAndResource(string fileName, bool ignoreScript, DirConfig dirConfig) { string htmlContent = this.html; if (!dirConfig.CheckLegal()) { throw new Exception("路径配置不在统一磁盘下"); } dirConfig.CreateDir(); if (string.IsNullOrEmpty(this.url)) { throw new Exception("未指定当前url"); } //是否忽略JS if (ignoreScript) { htmlContent = this.FilterScript(); } #region 保存所有css和css中引用的图片 htmlContent = RegexLibrary.RegCssLink.Replace(htmlContent, delegate(Match match) { //由于css文件里有可能引用图片,所以在此处需要自定义css文件下载函数,将css文件中的图片下载到本地然后替换css文件中的引用路径 return this.MatchUrl(match, dirConfig, this.url, dirConfig.HtmlDirPath, dirConfig.CssDirPath, "src", null, delegate(string cssUrl) { //css内容、css保存路径 string cssContent, cssSavePath = cssUrl; //请求css文件 //cRequest = Spider.CreateRequest(cssUrl); //cRequest.SetHeader("Referer", this.Url); //cResponse = Spider.Get(cRequest); HttpClient hc = new HttpClient(cssUrl); string content = hc.Request(); //如果请求成功 if (!string.IsNullOrEmpty(content)) { string tempFileName = hc.GetFileName(); //确定css文件保存的绝对路径 cssSavePath = dirConfig.UseWebSite ? Path.GetFullPath(PathUtility.GetSaveDir(this.Url, cssUrl, dirConfig.HtmlDirPath) + tempFileName) : Path.GetFullPath(dirConfig.CssDirPath + "\\" + tempFileName); //获取css内容 cssContent = content;// cResponse.GetContent(null, false); //下载css里引用的图片,并替换css内容中图片地址的引用 cssContent = this.ReplaceBackgroundUrl(cssContent, cssUrl, dirConfig, Path.GetDirectoryName(cssSavePath), dirConfig.ImgDirPath, false, null); //将css保存到本地 cssSavePath = FileUtility.SaveText(cssSavePath, cssContent, hc.Encoding); } //返回保存以后的css本地路径 return cssSavePath; }); }); #endregion #region 保存所有JS htmlContent = RegexLibrary.RegScriptLink.Replace(htmlContent, delegate(Match match) { return this.MatchUrl(match, dirConfig, this.url, dirConfig.HtmlDirPath, dirConfig.JsDirPath, "src", null, null); }); #endregion #region 保存所有图片 htmlContent = RegexLibrary.RegImg.Replace(htmlContent, delegate(Match match) { return this.MatchUrl(match, dirConfig, this.url, dirConfig.HtmlDirPath, dirConfig.ImgDirPath, "src", null, null); }); #endregion #region 保存所有Flash htmlContent = RegexLibrary.RegFlash.Replace(htmlContent, delegate(Match match) { return this.MatchUrl(match, dirConfig, this.url, dirConfig.HtmlDirPath, dirConfig.FlashDirPath, "src", null, null); }); #endregion #region 当前页面内嵌css中的图片 htmlContent = this.ReplaceBackgroundUrl(htmlContent, this.Url, dirConfig, dirConfig.HtmlDirPath, dirConfig.ImgDirPath, false, null); #endregion return FileUtility.SaveText(Path.GetFullPath(dirConfig.HtmlDirPath + "\\" + fileName), htmlContent, this.encode); }