Download remote Web files using XMLHTTP and ADODB.stream caching techniques _ Thieves/Collection

Source: Internet
Author: User

<%
'----------get content remotely and have the content on the local computer, including any files! ----------
'---------------use XMLHTTP and ADODB.stream-----------------
' On Error Resume Next
'-------------------------------defines the output format-----------------------------
Path=request ("path")
If Path = "" Then
Path= "Http://pcqc.86516.com/index.asp"
' Here is the definition of the URL is Baidu, note must have a file suffix
End If

spath = Path
If left (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)
End If
' Response.Write Err. Description
'--------------processing function-----------

Sub LocalFile (Path)
'-------------------simply jump to the page if it is a local file-------------------
' Response.Redirect Path
Response.Write "An error has occurred! "
End Sub
Sub RemoteFile (spath)
'-------------------------processing remote file Functions------------------------------
FileName = GetFileName (spath)
'-------------GetFileName the process of converting addresses to qualified file names-------------
filename = Server.MapPath ("cache/" & filename)
Set objFSO = Server.CreateObject ("Scripting.FileSystemObject")
' Response.Write FileName
If Objfso.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.Write Path
t = GetBody (Path)
'-----------------is written to the browser using the binary method--------------------------
Response.BinaryWrite T
Response.Flush
'-----------------Output buffer------------------------------------------
SaveFile T,getfilename (PATH)
'------------------cache the contents of the file to the local path for the next visit-----------
End If
Set objFSO = Nothing
End Sub

Function getbody (URL)
'-----------------------function for remote fetch of content---------------------
' On Error Resume Next
' Response.Write URL
Set retrieval = CreateObject ("Microsoft.XMLHTTP")
'----------------------set up the XMLHTTP object-----------------------------
With retrieval
. Open "Get", url, False, "", ""
'------------------send-----------------------with Get, asynchronous method
. Send
' GetBody =. ResponseText
GetBody =. Responsebody
'------------------function returns the contents of the Fetch--------------------------
End With
Set retrieval = Nothing
' Response. Write Err. Description
End Function

Function GetFileName (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
End Function

sub savefile (str,fname)
'-------------------------This function is the function to save the contents of the stream-------------------
' On Error Resume Next
Set objstream = Server.CreateObject (' ADODB. Stream ")
'--------------set up the ADODB.stream object, you must have more than ADO 2.5 version---------
' objStream.Type = adTypeBinary
objStream.Type = 1
'-------------open in binary mode-------------------------------------
objStream.Open
Objstream.write str
'--------------------writes the string contents to the buffer--------------------------
' response. Write fname
Path attention
Objstream. SaveToFile "E:\webroot\pcqc\vip\UploadFile\cache\" &fname,2
' objstream. SaveToFile "d:\cache\" & Fname,adsavecreateoverwrite
'-------------------- Writes the buffered content to the file--------------------------
' response. BinaryWrite objstream. Read
Objstream. Close () the
Set objstream = Nothing
-----------------------Closes the object, releasing the resource-------------------------
' response. Write Err. Description
End Sub

function saveimage (from,tofile)
Dim Geturl,objstream,imgs
Geturl=trim (from)
Imgs=gethttppage (Geturl) ' Gets a picture of the content of the process
Set objstream = Server.CreateObject ("ADODB. Stream ")" To create the ADODB.stream object, you must have ADO 2.5 above version
objStream.Type = 1 ' Open in binary mode
objStream.Open
Objstream.write IMGs ' writes the string contents to the buffered
Objstream. SaveToFile Server.MapPath (ToFile), 2 '-writes the buffered content to the file
objstream. Close () ' closes object
set objstream=nothing
End Function
%>

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.