asp读取远程文件并保存到本地代码
程序员文章站
2023-11-21 17:11:16
<% dim act,iflogin,loginurl,loginacturl,logindata,fileurl,filename act=request...
<%
dim act,iflogin,loginurl,loginacturl,logindata,fileurl,filename
act=request.querystring("act")
if(act="do") then
iflogin=cint(request.querystring("iflogin"))
loginacturl=request.form("loginacturl")
logindata=request.form("logindata")
fileurl=request.form("fileurl")
filename=mid(fileurl,instrrev(fileurl,"/")+1)
extpos=instrrev(filename,"?")
if(extpos>0) then
filename=left(filename,extpos-1)
end if
if(iflogin=1) then
call posthttppage(loginacturl,logindata)
end if
call downloadfile(fileurl,filename)
else
%>
<form name="form1" method="post" action="?act=do">
<p>是否登录:
<input name="iflogin" type="radio" value="1">
是
<input name="iflogin" type="radio" value="0" checked>
否 </p>
<p>登录提交地址:
<input name="loginacturl" type="text" size="50">
</p>
<p>
登录数据:<input name="logindata" type="text" size="50">
</p>
<p>
要保存的远程文件url:
<input name="fileurl" type="text" size="50">
</p>
<p>
<input type="submit" name="submit" value="提交">
<input type="reset" name="submit2" value="重写">
</p>
</form>
<%
end if
function bytestobstr(body,code)
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 =code
bytestobstr = objstream.readtext
objstream.close
set objstream = nothing
end function
function posthttppage(loginacturl,postdata)
dim xmlhttp
dim retstr
set xmlhttp = createobject("microsoft.xmlhttp")
xmlhttp.open "post", loginacturl, false
xmlhttp.setrequestheader "content-length",len(postdata)
xmlhttp.setrequestheader "content-type", "application/x-www-form-urlencoded"
xmlhttp.send postdata
if err.number <> 0 then
set xmlhttp=nothing
response.write("提交登录时出错!提交数据:"&postdata)
exit function
end if
posthttppage=bytestobstr(xmlhttp.responsebody,"gb2312")
set xmlhttp = nothing
end function
'''''''''''''''''''''''''''''''''''''''''''
function downloadfile(url,filename)
set xml = server.createobject("msxml2.xmlhttp") '创建对象
xml.open "get",url,false
xml.send '发送请求
if err.number>0 then
response.status="404"
else
response.contenttype="application/octet-stream"
response.addheader "content-disposition:","attachment; filename=" & filename
range=mid(request.servervariables("http_range"),7)
if range="" then
response.binarywrite(xml.responsebody)
else
s.position=clng(split(range,"-")(0))
response.binarywrite(xml.responsebody)
end if
end if
response.end
set xml = nothing
end function
'''''''''''''''''''''''''''''''''''''''''''''''''''''
%>
dim act,iflogin,loginurl,loginacturl,logindata,fileurl,filename
act=request.querystring("act")
if(act="do") then
iflogin=cint(request.querystring("iflogin"))
loginacturl=request.form("loginacturl")
logindata=request.form("logindata")
fileurl=request.form("fileurl")
filename=mid(fileurl,instrrev(fileurl,"/")+1)
extpos=instrrev(filename,"?")
if(extpos>0) then
filename=left(filename,extpos-1)
end if
if(iflogin=1) then
call posthttppage(loginacturl,logindata)
end if
call downloadfile(fileurl,filename)
else
%>
<form name="form1" method="post" action="?act=do">
<p>是否登录:
<input name="iflogin" type="radio" value="1">
是
<input name="iflogin" type="radio" value="0" checked>
否 </p>
<p>登录提交地址:
<input name="loginacturl" type="text" size="50">
</p>
<p>
登录数据:<input name="logindata" type="text" size="50">
</p>
<p>
要保存的远程文件url:
<input name="fileurl" type="text" size="50">
</p>
<p>
<input type="submit" name="submit" value="提交">
<input type="reset" name="submit2" value="重写">
</p>
</form>
<%
end if
function bytestobstr(body,code)
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 =code
bytestobstr = objstream.readtext
objstream.close
set objstream = nothing
end function
function posthttppage(loginacturl,postdata)
dim xmlhttp
dim retstr
set xmlhttp = createobject("microsoft.xmlhttp")
xmlhttp.open "post", loginacturl, false
xmlhttp.setrequestheader "content-length",len(postdata)
xmlhttp.setrequestheader "content-type", "application/x-www-form-urlencoded"
xmlhttp.send postdata
if err.number <> 0 then
set xmlhttp=nothing
response.write("提交登录时出错!提交数据:"&postdata)
exit function
end if
posthttppage=bytestobstr(xmlhttp.responsebody,"gb2312")
set xmlhttp = nothing
end function
'''''''''''''''''''''''''''''''''''''''''''
function downloadfile(url,filename)
set xml = server.createobject("msxml2.xmlhttp") '创建对象
xml.open "get",url,false
xml.send '发送请求
if err.number>0 then
response.status="404"
else
response.contenttype="application/octet-stream"
response.addheader "content-disposition:","attachment; filename=" & filename
range=mid(request.servervariables("http_range"),7)
if range="" then
response.binarywrite(xml.responsebody)
else
s.position=clng(split(range,"-")(0))
response.binarywrite(xml.responsebody)
end if
end if
response.end
set xml = nothing
end function
'''''''''''''''''''''''''''''''''''''''''''''''''''''
%>
上一篇: django celery redis使用具体实践
下一篇: ASP使用FSO读取模板的代码
推荐阅读