一、目前的静态页生成方法有简单的模板替换、常见的ASP+FSO等,这里给大家介绍一种更简单的方法。原理就是借助XMLHTTP对象获取目标页面的源代码,然后写入到静态网页文件中。代码如下:
Code
Dim filename,fso,fout
filename="index.html"
![](https://yqfile.alicdn.com/img_a6339ee3e57d1d52bc7d02b338e15a60.gif?x-oss-process=image/resize,w_1400/format,webp)
Set fso=server.CreateObject("Scripting.FileSystemObject")
path=server.MapPath(filename)
Set fout=fso.CreateTextFile(path)
fout.WriteLine("<!--This page is created by program on "&now&" automatically-->")
![](https://yqfile.alicdn.com/img_a6339ee3e57d1d52bc7d02b338e15a60.gif?x-oss-process=image/resize,w_1400/format,webp)
webstr = getHTTPPage("http://url")
fout.WriteLine(webstr)
fout.close
set fout=nothing
set fso=nothing
![](https://yqfile.alicdn.com/img_a6339ee3e57d1d52bc7d02b338e15a60.gif?x-oss-process=image/resize,w_1400/format,webp)
'生成后让网页自动关闭
Response.Write("<script>")
Response.Write("function ToClose(){")
Response.Write("window.opener=null;window.close();}")
Response.Write("setTimeout(ToClose,10000);")
Response.Write("</script>")
![](https://yqfile.alicdn.com/img_a6339ee3e57d1d52bc7d02b338e15a60.gif?x-oss-process=image/resize,w_1400/format,webp)
'获取目标网页的源代码
Function getHTTPPage(url)
dim Http
set Http=server.createobject("MSXML2.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
if len(Http.responseBody)<1000 then
Response.End()
end if
set http=nothing
if err.number<>0 then err.Clear
End Function
![](https://yqfile.alicdn.com/img_a6339ee3e57d1d52bc7d02b338e15a60.gif?x-oss-process=image/resize,w_1400/format,webp)
'字符转换,解决中文乱码问题
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文件里,然后在另一网页中使用JS调用定时程序,当然还有另外一种方法,就是用windows的任务计划,这里的方法是把下面代码写入一静态页中,然后在浏览器打开此网页就可以了
<
script
>
function
run(){
window.open(
'
make_html.asp
'
,
'
_blank
'
);}
setInterval(run,
5000
);
<
/
script>
二、这里顺便说下在JavaScript中使用XMLHttpRequest对象获取网页代码的方法,在返回中文的时候会出现乱码
原因是:
1、xtmlhttp 返回的数据默认的字符编码是utf-8,如果客户端页面是gb2312或者其它编码就会产生乱码
2、post方法提交的数据默认字符编码也是utf-8,如果服务器端是gb2312或其他编码数据就会产生乱码
解决方法:
1、若客户端是gb2312编码,则在服务器指定输出流编码
Response.ContentType
=
"
text/html
"
Response.Charset
=
"
GB2312
"
2、服务器端和客户端都使用utf-8编码
三、还有一个常见的编码问题是URL编码解码问题,下面使用JavaScript实现asp中的UrlEncode和UrlDecode功能,这里也可以学到JavaScript如何调用VBscript的函数
<
script language
=
"
vbscript
"
>
Function
str2asc(strstr)
str2asc
=
hex
(
asc
(strstr))
End Function
Function
asc2str(ascasc)
asc2str
=
chr
(ascasc)
End Function
</
script
>
Code
<script language="javascript">
![](https://yqfile.alicdn.com/img_405b18b4b6584ae338e0f6ecaf736533.gif?x-oss-process=image/resize,w_1400/format,webp)
function UrlEncode(str)
{
var ret="";
var strSpecial="!\"#$%&'()*+,/:;<=>?[]^`{|}~%";
for(var i=0;i<str.length;i++){
var chr = str.charAt(i);
var c=str2asc(chr);
if(parseInt("0x"+c) > 0x7f){
ret+="%"+c.slice(0,2)+"%"+c.slice(-2);
}else{
if(chr==" ")
ret+="+";
else if(strSpecial.indexOf(chr)!=-1)
ret+="%"+c.toString(16);
else
ret+=chr;
}
}
return ret;
}
function UrlDecode(str){
var ret="";
for(var i=0;i<str.length;i++){
var chr = str.charAt(i);
if(chr == "+"){
ret+=" ";
}else if(chr=="%"){
var asc = str.substring(i+1,i+3);
if(parseInt("0x"+asc)>0x7f){
ret+=asc2str(parseInt("0x"+asc+str.substring(i+4,i+6)));
i+=5;
}else{
ret+=asc2str(parseInt("0x"+asc));
i+=2;
}
}else{
ret+= chr;
}
}
return ret;
}
tar_str="爱情"
encode_str=UrlEncode(tar_str)
decode_str=UrlDecode(encode_str)
document.write("编码后:"+encode_str+"<br>");
document.write("解码后:"+decode_str);
</script>