程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> 關於ASP編程 >> asp定時生成靜態HTML的代碼

asp定時生成靜態HTML的代碼

編輯:關於ASP編程

復制代碼 代碼如下:
<%
'判斷是否要生成新的HTML
if Application("cache_asptohtml_date")="" then
Application.Lock
Application("cache_asptohtml_date")=now()
Application.Unlock
Call aspTohtml
Response.Redirect("index.html")
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.html")
Else
Response.Redirect("index.html")
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.html")
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
%>

ASP定時生成靜態頁(HTML)2
復制代碼 代碼如下:
<%
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" '//生成的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" '//生成的HTML文件名
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
%>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved