/// <summary> /// 生成静态页面 /// </summary> /// <param name="webUrl">url</param> /// <param name="savePath">保存本地的路径</param> /// <returns></returns> public static void AppendHTML(string webUrl, string savePath) { Dictionary<string, string> dict = new Dictionary<string, string>(1); dict.Add(webUrl, savePath); AppendHTML(dict); } public static void AppendHTML(Dictionary<string, string> dict) { WebClient client = new WebClient(); client.Credentials = CredentialCache.DefaultCredentials; //获取或设置用于对向Internet资源的请求进行身份验证的网络凭据。 HttpContext context = HttpContext.Current; foreach (KeyValuePair<string, string> pair in dict) { string url = pair.Key.StartsWith("http://") ? pair.Key : "http://" + context.Request.ServerVariables["HTTP_HOST"] + pair.Key; string path = pair.Value; path = Path.GetDirectoryName(AppRuntime.MapPath(path)); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } byte[] buffer = client.DownloadData(url); long length = buffer.LongLength; if (length > int.MaxValue) { throw new IOException("FileTooLong2GB"); } int count = (int)length; using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write)) using (BinaryWriter writer = new BinaryWriter(stream, Encoding.Default)) { writer.Write(buffer, 0, count); } } client.Dispose(); }