静态页面对网站的好处是多方面的,HTML静态首页可以提升网站排名,提高网站打开速度,编程之家下面跟大家分享:ASP利用XMLHTTP生成HTML静态首页的方法。
ASP利用XMLHTTP生成HTML静态首页的代码
<%
'判断是否要生成新的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
'-----------------------------------------------
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
%>