[table=90%][tr][td=3,1]ASP静态化专题系列一
来源:中国供应礼品网原创技术 时间 2009-2-14 [/td][/tr][tr][td=3,1]
ASP静态化专题系列一先讲思路:读取html代码,写入html代码。
主要技术:Microsoft.XMLHTTP,adodb.stream,Scripting.FileSystemObject。
如果是初学者,可能一时半会没能看懂我的意思。也不能担心,慢慢来,会用就行,不一定要搞清楚某些代码的功能和含义。请看代码一:
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
这里就是读客户端的html代码。注意一点就是:如果是中文站就还要用到一代码:
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
其中的GB2312说明被访问的网站的编码是GB2312,如果编码是其它的如UTF-8就换为UTF-8了。
到现在有了读取代码的功能了,还差写入了,这里用到FSO的强大功能。代码如下:
Set fs=Server.CreateObject("Scripting.FileSystemObject")
If (fs.FileExists(server.MapPath("./")&"index.html") Then
fs.DeleteFile(server.MapPath("./")&"index.html")
End If Set CrFi=fs.CreateTextFile(server.MapPath("./")&"index.html")
Crfi.Writeline(wstr)
set CrFi=nothing
set fs=nothing
这段代码是在服务端根目录下生成一个名为index的html文件,应该有朋友会注意这是在根目录生成静态首页常用的方法。
三段主要代码给写出来了,有的朋友可能还不会用,我就把完整的代码写出来 |