首页 > 开发 > Asp > 正文

利用xmlhttp和adodb.stream加缓存技术下载远程Web文件

2020-06-20 14:09:31
字体:
来源:转载
供稿:网友

<%
'----------远程获取内容,并将内容存在本地电脑上,包括任何文件!----------
'---------------利用xmlhttp和adodb.stream-----------------
'On Error Resume Next
'-------------------------------定义输出格式-----------------------------
path=request("path")
if path ="" then
path="http://pcqc.86516.com/index.asp"
'这里定义的网址是百度,,注意一定要有文件后缀
end if

sPath = Path
if left(lcase(path),7) <> "http://" then
'-------------如果前面没有http就是本地文件,交给LocalFile处理------------
LocalFile(path)
else
'--------------------否则为远程文件,交给RemoteFile处理------------------
RemoteFile(Path)
end if
'Response.Write err.Description
'--------------处理函数-----------

sub LocalFile(Path)
'-------------------如果为本地文件则简单的跳转到该页面-------------------
'Response.Redirect Path
Response.write "发生错误!"
End Sub
Sub RemoteFile(sPath)
'-------------------------处理远程文件函数------------------------------
FileName = GetFileName(sPath)
'-------------GetFileName为把地址转换为合格的文件名过程-------------
FileName = Server.MapPath("Cache/" & FileName)
Set objFso = Server.CreateObject("Scripting.FileSystemObject")
'Response.Write fileName
if objFso.FileExists(FileName) Then
'--------------检查文件是否是已经访问过,如是,则简单跳转------------
Response.Redirect "cache/" & GetFileName(path)
Else
'----------------否则的话就先用GetBody函数读取----------------------
'Response.Write Path
t = GetBody(Path)
'-----------------用二进制方法写到浏览器上--------------------------
Response.BinaryWrite t
Response.Flush
'-----------------输出缓冲------------------------------------------
SaveFile t,GetFileName(path)
'------------------将文件内容缓存到本地路径,以待下次访问-----------
End if
Set objFso = Nothing
End Sub

Function GetBody(url)
'-----------------------本函数为远程获取内容的函数---------------------
'on error resume next
'Response.Write url
Set Retrieval = CreateObject("Microsoft.XMLHTTP")
'----------------------建立XMLHTTP对象-----------------------------
With Retrieval
.Open "Get", url, False, "", ""
'------------------用Get,异步的方法发送-----------------------
.Send
'GetBody = .ResponseText
GetBody = .ResponseBody
'------------------函数返回获取的内容--------------------------
End With
Set Retrieval = Nothing
'response.Write err.Description
End Function

Function GetFileName(str)
'-------------------------本函数为合格化的文件名函数-------------------
str = Replace(lcase(str),"http://","")
str = Replace(lcase(str),"//","/")
str = Replace(str,"?","")
str = Replace(str,"&","")
str = Replace(str,"/","")
str = replace(str,vbcrlf,"")
GetFileName = str

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表