您所在的位置:小祥子 » 编程 » ASP » 正文

ASP定时生成HTML方法

时间:2015-02-01 编辑:佚名 来源:互联网

今天介绍两种生成定时生成HTML的方法,主要是首页的生成,其他页面的HTML也一样,通过FSO对比文件时间,判断时候需要生成

<%
'判断是否要生成新的HTML
if Application("cache_ASPtohtml_date")="" then
Application.Lock
Application("cache_asptohtml_date")=now()
Application.Unlock
Call aspTohtml
Response.Redirect("index.htm")
end if
if DateDiff("s", Application("cache_asptohtml_date"),Now)> 100 then '比较上次生成的时间与本次时间相差了多少秒
Application.Lock
Application("cache_asptohtml_date")=now()
Application.UnLock
Call aspTohtml
Response.Redirect("index.htm")
Else
Response.Redirect("index.htm")
End if
'获取当前路径
function getpath
if Request.ServerVariables("SERVER_PORT")<>"80" then
UserUrl = "http://"&Request.ServerVariables("SERVER_NAME")& ":" & Request.ServerVariables("SERVER_PORT")& Request.ServerVariables("URL")
else
UserUrl = "http://"&Request.ServerVariables("SERVER_NAME")& Request.ServerVariables("URL")
end if
getpath=left(UserUrl,InstrRev(UserUrl,"/"))
end function
sub aspTohtml
'-----------------------------------------------
'使用XMLHTTP生成静态首页
'Curl 为你的首页地址,需要你的空间支持FSO
dim read,Curl,content
Curl=getpath&"home.asp"
read=getHTTPPage(Curl)
if read<>"" then
content=read
Set Fso = Server.CreateObject("Scripting.FileSystemObject")
Filen=Server.MapPath("index.htm")
Set Site_Config=FSO.CreateTextFile(Filen,true, False)
Site_Config.Write content
Site_Config.Close
Set Fso = Nothing
end if
End sub
Function getHTTPPage(url)
dim http
set http=Server.createobject("Microsoft.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
set http=nothing
if err.number<>0 then err.Clear
End Function
Function BytesToBstr(body,Cset)
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 = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
%>

<%
set fs=server.createobject("scripting.filesystemobject")
file=server.mappath("time.txt")
set txt=fs.opentextfile(file,1,true)
if not txt.atendofstream then
times=txt.ReadLine
Else
response.write "<br /><!--有没有发现time.txt 开始生成!-->"
HtmlPage = "time.txt" '要生成首页的HTML文件名
Template = NOW()
Set FSO = Server.CreateObject ("Scripting.FileSystemObject")
Set FileOut = FSO.CreateTextFile(Server.MapPath (HtmlPage))
FileOut.WriteLine Template
FileOut.Close
Set FSO = Nothing
end If
If datediff("s",times,NOW()) > 3600 Then '//上次生成到现在的时间,若大于 3600秒则重新生成
response.write "<br /><!--时间过了开始更新-->"
code = "这里是生成html文件的内容"
'FSO生成HTML
HtmlPage = "index.html"
Template = code
Set FSO = Server.CreateObject ("Scripting.FileSystemObject")
Set FileOut = FSO.CreateTextFile(Server.MapPath (HtmlPage))
FileOut.WriteLine Template
FileOut.Close
Set FSO = Nothing
'FSO生成time.txt文件
HtmlPage = "time.txt"
Template = NOW()
Set FSO = Server.CreateObject ("Scripting.FileSystemObject")
Set FileOut = FSO.CreateTextFile(Server.MapPath (HtmlPage))
FileOut.WriteLine Template
FileOut.Close
Set FSO = Nothing
Else
response.write "<br /><!-- 已经过去了"&datediff("s",times,NOW())&"秒!-->"
End If
%>

关键词:定时 生成 HTML