Downloading remote Web files using XMLHTTP and ADODB.stream caching techniques

Source: Internet
Author: User
ado|stream|web|xml| Cache | Download research for several days ADODB.stream and XMLHTTP applications,
I've found a lot of interesting tutorials,
The following code is to save a remote page, a picture address, to a local instance.
Save the code as one. ASP's files,
Put it in a directory in IIS and see a cache directory at its superiors,
The downloaded files will be saved to the cache directory

Fso. Asp?path=logo.gif
You can save the logo.gif file.
A friend who learns thieves can also see.

<%
'----------get content remotely and have the content on the local computer, including any files! ----------
'---------------use XMLHTTP and ADODB.stream-----------------
' Onerrorresumenext
'-------------------------------defines the output format-----------------------------
Path=request ("path")
Ifpath= "" Then
Path= "Http://www.baidu.com/index.html"
' Here is the definition of the URL is Baidu, note must have a file suffix
endif

Spath=path
Ifleft (LCase (path), 7) <> "http://" then
'-------------if HTTP is not in front of the local file, give LocalFile processing------------
LocalFile (PATH)
Else
'--------------------Otherwise for remote files, to RemoteFile processing------------------
RemoteFile (Path)
endif
' Response.Writeerr.Description
'--------------processing function-----------

Sublocalfile (Path)
'-------------------simply jump to the page if it is a local file-------------------
' Response.redirectpath
Response.Write "An error has occurred! "
Endsub

Subremotefile (spath)
'-------------------------processing remote file Functions------------------------------
Filename=getfilename (spath)
'-------------GetFileName the process of converting addresses to qualified file names-------------
Filename=server.mappath ("/cache/" &filename)
Setobjfso=server.createobject ("Scripting.FileSystemObject")
' Response.writefilename
Ifobjfso.fileexists (FileName) Then
'--------------Check to see if the file is already visited, and if so, simply jump------------
Response.Redirect "/cache/" &getfilename (Path)
Else
'----------------Otherwise, read the GetBody function First----------------------
' Response.writepath
T=getbody (Path)
'-----------------is written to the browser using the binary method--------------------------
Response.binarywritet
Response.Flush
'-----------------Output buffer------------------------------------------
Savefilet,getfilename (PATH)
'------------------cache the contents of the file to the local path for the next visit-----------
Endif
Setobjfso=nothing
Endsub

Functiongetbody (URL)
'-----------------------function for remote fetch of content---------------------
' Onerrorresumenext
' Response.writeurl
Setretrieval=createobject ("Microsoft.XMLHTTP")
'----------------------set up the XMLHTTP object-----------------------------
Withretrieval
. Open "Get", Url,false, "" "," "
'------------------send-----------------------with Get, asynchronous method
. Send
' Getbody=. ResponseText
Getbody=. Responsebody
'------------------function returns the contents of the Fetch--------------------------
Endwith
Setretrieval=nothing
' Response. Writeerr.description
Endfunction

Functiongetfilename (str)
'-------------------------This function is a qualified filename function-------------------
Str=replace (LCase (str), "http://", "")
Str=replace (LCase (str), "//", "/")
Str=replace (str, "?", "")
Str=replace (str, "&", "")
Str=replace (str, "/", "")
Str=replace (Str,vbcrlf, "")
Getfilename=str
Endfunction

Subsavefile (Str,fname)
'-------------------------this function to save the contents of the stream-------------------
' Onerrorresumenext
Setobjstream=server.createobject ("ADODB.") Stream ")
'--------------set up the ADODB.stream object, you must ADO2.5 the above version---------
' Objstream.type=adtypebinary
Objstream.type=1
'-------------open in binary mode-------------------------------------
objStream.Open
Objstream.writestr
'--------------------write the string contents to the buffer--------------------------
' Response. Writefname
' Path note
Objstream. SaveToFile "d:/web/cache/" &fname,2
' Objstream. SaveToFile "D:cache" &fname,adsavecreateoverwrite
'--------------------write buffered content to the file--------------------------
' Response. Binarywriteobjstream.read
Objstream. Close ()
Setobjstream=nothing
'-----------------------close the object and release the resource-------------------------
' Response. Writeerr.description
Endsub

Functionsaveimage (From,tofile)
Dimgeturl,objstream,imgs
Geturl=trim (from)
Imgs=gethttppage (Geturl) ' The process of obtaining the content of a picture
Setobjstream=server.createobject ("ADODB.") Stream ")" To create the ADODB.stream object, you must ADO2.5 the above version
Objstream.type=1 ' Open in binary mode
objStream.Open
Objstream.writeimgs ' writes string contents to buffer
Objstream. Savetofileserver.mappath (ToFile), 2 '-write buffered content to file
Objstream. Close () ' Closes object
Setobjstream=nothing
Endfunction
%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.