直接通过访问页面获取html代码实现静态化
突然想到一个静态化页面的方法:直接保存源代码即可。
模拟浏览器访问,获得源码,写入文件。不知道是否存在安全风险;各位大神请指点:
注意
1、资源使用绝对路径,包括图片,JS,CSS和其他资源。
2、html文件可能需要统一管理(按目录/日期分类保存,使用了觉得路径可以避免资源失效)。
示例代码
string file = Server.MapPath("/") + "index.html"; Response.Write(file); //Response.End(); //return; WebClient web = new WebClient(); byte[] buffer = web.DownloadData("http://localhost:81/"); string result = Encoding.UTF8.GetString(buffer); FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write); StreamWriter sw = new StreamWriter(fs, Encoding.UTF8); sw.Write(result); sw.Flush(); sw.Close(); fs.Close(); Response.Redirect("http://localhost:81/index.html");