将ASP动态网页转换成和生成HTML静态页面的方法


代码适用范围:网站的某个或者某些页面访问量比较大,经常读取数据库,给服务器增加负担,访问速度也比较慢。如果该页面调用的数据库较多,但网站内容不经常更新,就可以直接将该页面生成HTML网页,等下次内容更新的时候,再生成一次。或者写一个代码,隔一段时间生成一次。

源代码:
<form method="post" action="?">
<div align="center">
<input name="asp2html" type="submit" value="生成静态主页"/>
</div>
</form>
<%
If request.Form("asp2html")<>"" Then
%>

<%
Function GetPage(url)
'获得文件内容
dim Retrieval
Set Retrieval = CreateObject("Microsoft.XMLHTTP")
With Retrieval
  .Open "Get", url, False ', "", ""
  .Send
  GetPage = BytesToBstr(.ResponseBody)
End With
Set Retrieval = Nothing
End Function

Function BytesToBstr(body)
dim objstream
set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = "GB2312"
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function

on error resume next
Url="http://www.xxx.com/index.asp" '要读取的页面地址
response.write "开始更新首页...<BR>"
wstr = GetPage(Url)

'response.write(wstr)
Set fs=Server.CreateObject("Scripting.FileSystemObject")

'if not MyFile.FolderExists(server.MapPath("/html/")) then
'MyFile.CreateFolder(server.MapPath("/html/"))'
'end if

'要存放的页面地址
dizhi=server.MapPath("index.htm")
If (fs.FileExists(dizhi)) Then
fs.DeleteFile(dizhi)
End If

Set CrFi=fs.CreateTextFile(dizhi)
Crfi.Writeline(wstr)
set CrFi=nothing
set fs=nothing
response.write "...<font color=red>更新完成!</font>"
%>

<%
End If
%>

直接保存成一个asp文件,上传到网站运行即可。注意,需要把URL(要转化的asp地址)和dizhi(要保存的html地址)设置好,一般这两个文件在同一个目录,才能保证图片或者css、js起作用。


评论: 0 | 引用: 0 | 查看次数: 12247
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1 字 | UBB代码 关闭 | [img]标签 关闭