asp定时生成静态HTML的代码
<%@Language="vbscript" Codepage="65001" %> <!-- 中文显示 -->
<%
' 思路,定义一个时间戳的变量,假设为a
' 1, 没有a这个变量
' 1.1 定义一个时间戳变量a
' 1.2 生成index.html页面
' 1.2.1 获取你想要显示的模板页面的地址
' 1.2.2 结果 获取模板页面的内容
' 步骤 总结,将模板页面以数据流的形式或者说一个字符串的形式赋值给一个变量
' 1,通过 Microsoft.XMLHTTP发送一个请求,获取内容http.responseBody
' 2,将获取的内容创建一个数据流,以utf-8的格式写入,并且返回
' 1.2.3 创建一个新的页面 ,将获取的模板页面的内容写进去
'
'
' 1.3 跳转index.html
' 2, 有a这个变量
' 2.1 间隔时间在10s之内
' 直接跳转index.html
' 2.2 间隔时间不在10s之内
' 重复 1 步骤
'判断是否要生成新的HTML , 是否存在-如果存在相差时间是否在100s
if Application("cache_asptohtml_date")="" then
' 如果 Application("cache_asptohtml_date") 变量不存在
Application.Lock 'Lock 方法阻止其他用户修改 Application 对象中的变量 (用来确保同一时间只有一位用户可修改 Application 变量)。
Application("cache_asptohtml_date")=now()
Application.Unlock 'Unlock 方法使其他用户能够修改存储在 Application 对象中的变量 (在它被使用 Lock 方法锁定之后)。
Call aspTohtml
Response.Redirect("index.html") '直接跳转到index.html
end if
' 如果 Application("cache_asptohtml_date") 变量存储的时间于现在的时间相差100s
if DateDiff("s", Application("cache_asptohtml_date"),Now)> 1 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 ' 如果当前端口不是80
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,"/")) ' 从字符串的左侧返回 找到结尾为 / 的字符第一次出现的位置InstrRev(UserUrl,"/")=下标 数目的字符。
end function
sub aspTohtml
'----------------------------------------------------------
'使用XMLHTTP生成静态首页的代码
'Curl 为你的想要生成的页面
'-----------------------------------------------------------
dim read,Curl,content
Curl=getpath&"model.html" ' http://localhost:8081/model.html
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
'在asp页面发送一个url请求,获该页面的内容
Function getHTTPPage(url)
dim http
set http=Server.createobject("Microsoft.XMLHTTP")
http.open "GET",url,false '想url发送一个请求,和ajax类式,但是这里False表示同步
http.send()
if http.readystate<>4 then
exit function ' 如果请求状态不为4 退出这个函数
end if
Response.Write http.responseBody
getHTTPPage= bytesToBSTR(http.responseBody,"UTF-8") ' 创建一个文件 -> 将http.responseBody以utf-8的格式写入
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 'Object.ReadText(NumChars) 指定的要读取的找度,不指定则读取全部。
objstream.Close
set objstream = nothing
End Function
%>
第二种方式
<%@Language="vbscript" Codepage="65001" %> <!-- 中文显示 -->
<%
' 思路
' 1,判断time.txt是否有
' 1.1 是
' 读取time.txt当前行(写入的时间)
'
' 1.2 否
' 创建 time.txt,写入当前时间
' 2,时间相差大于多少
' 2.1 是
' 生成indnx.html 页面,将相应的字符串写入
' 重新生成time.txt覆盖,同 1.2
'
' 2.2 否
' 提示距离更新还有多少
set fs=server.createobject("scripting.filesystemobject")
file=server.mappath("time.txt")
set txt=fs.opentextfile(file,1,true) '法打开指定的文件,并返回可用来访问此文件的 TextStream 对象。
if not txt.atendofstream then ' 如果文件指针位于 TextStream 文件的末尾,AtEndOfStream 属性返回 True,否则返回 False。注意:该属性仅工作于以只读方式打开的 TextStream 对象。
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)) 'MapPath 方法可把指定的路径影射到服务器上相应的物理路径上。
FileOut.WriteLine Template
FileOut.Close
Set FSO = Nothing
end If
If datediff("s",times,NOW()) > 30 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
%>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构