网络编程
位置:首页>> 网络编程>> Asp编程>> ASP生成静态网页各种方法收集整理(2)

ASP生成静态网页各种方法收集整理(2)

  发布时间:2008-02-18 19:25:00 

标签:静态,静态网页,fso,xmlhttp,adodb

4.自动按模板生成网站首页


<%
Response.Expires = 0 
Response.expiresabsolute = Now() - 1 
Response.addHeader "pragma", "no-cache" 
Response.addHeader "cache-control", "private" 
Response.CacheControl = "no-cache" 
Response.Buffer = True 
Response.Clear
Server.ScriptTimeOut=999999999
on error resume next 
'***************************************************************
'*                        定义 从模板从读取首页 函数
'* 说明:模板文件名为:index_Template.asp
'***************************************************************
Function GetPage(url) 
        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

'***************************************************************
'* 生页首页,文件名为:default.htm
'***************************************************************
dim Tstr
Tstr = GetPage("http://www.adhome.net/index_Template.asp")
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fout = fso.CreateTextFile(Server.MapPath(".")&"/default.htm")
fout.Write Tstr
fout.close
   Response.write"<script>alert(""生成首页成功!\n\n文件名为:default.htm"");location.href="http://www.adhome.net";</script>"
   Response.end
%>

5.将asp页面转换成htm页面

<%
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.sina.com.cn"'要读取的页面地址
response.write "开始更新首页..."
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>"
%>

代码算是最简单的,直接保存成一个asp文件即可,只要把URL(要转化的asp地址)和dizhi(要保存的html地址)设置好就可以了,一般这两个文件在同一个目录,才能保证图片或者css、js起作用。

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com