Attribute VB_Name = "modDownload" Option Explicit '============================================================================================================= ' ' modDownload Module ' ------------------ ' ' Created By : Kevin Wilson ' http://www.TheVBZone.com ( The VB Zone ) ' http://www.TheVBZone.net ( The VB Zone .net ) ' ' Last Update : May 23, 2003 ' Created On : January 15, 2003 ' ' VB Versions : 5.0 / 6.0 ' ' Requires : MSINET.OCX (Microsoft Internet Transfer Control) [REFERENCE this file like a DLL, do not add it to your project's "Components" like an ActiveX control] ' ' Description : This class wraps the MS INET control to give you an easy to use interface to download files ' from the internet via a variety of standard internet protocols. ' ' NOTE : I originally wrote this code so that the "objINET" variable was created at a PRIVATE level ' with events, so the "StateChanged" event could be reported back, but only one state was ever ' reported back, so going through the extra code and hastle was not worth the effort. ' ' Example Use : ' ' Option Explicit ' Private Sub Form_Load() ' Dim abytReturn() As Byte ' Dim lngResponseC As Long ' Dim strResponseI As String ' Dim lngErrNum As Long ' Dim strErrSrc As String ' Dim strErrDesc As String ' If DownloadFile("http://javaboutique.internet.com/AcuteScroller/AcuteScroller.class", _ ' abytReturn, p_HTTP, 80, , , , , lngResponseC, strResponseI, _ ' lngErrNum, strErrSrc, strErrDesc) = False Then ' MsgBox "The following error occured:" & Chr(13) & Chr(13) & _ ' "Error Number = " & CStr(lngErrNum) & Chr(13) & _ ' "Error Source = " & strErrSrc & Chr(13) & _ ' "Error Description = " & strErrDesc & Chr(13) & _ ' "Response Code = " & CStr(lngResponseC) & Chr(13) & _ ' "Response Information = " & strResponseI, vbOKOnly + vbExclamation, " " ' Else ' Open "C:\TEST.CLASS" For Binary Access Write As #1 ' Put #1, , abytReturn ' Close #1 ' Erase abytReturn ' End If ' End Sub ' '============================================================================================================= ' ' LEGAL: ' ' You are free to use this code as long as you keep the above heading information intact and unchanged. Credit ' given where credit is due. Also, it is not required, but it would be appreciated if you would mention ' somewhere in your compiled program that that your program makes use of code written and distributed by ' Kevin Wilson (www.TheVBZone.com). Feel free to link to this code via your web site or articles. ' ' You may NOT take this code and pass it off as your own. You may NOT distribute this code on your own server ' or web site. You may NOT take code created by Kevin Wilson (www.TheVBZone.com) and use it to create products, ' utilities, or applications that directly compete with products, utilities, and applications created by Kevin ' Wilson, TheVBZone.com, or Wilson Media. You may NOT take this code and sell it for profit without first ' obtaining the written consent of the author Kevin Wilson. ' ' These conditions are subject to change at the discretion of the owner Kevin Wilson at any time without ' warning or notice. Copyright© by Kevin Wilson. All rights reserved. ' '============================================================================================================= Public Enum Protocols p_Unknown = 0 'Unknown p_Default = 1 'Default Protocol p_FTP = 2 'FTP (File Transfer Protocol) p_Gopher = 3 'Gopher p_HTTP = 4 'HTTP (HyperText Transfer Protocol) p_HTTPS = 5 'Secure HTTP End Enum Private objINET As InetCtlsObjects.Inet 'Private objINET As Object Public Sub CancelDownload() If Not objINET Is Nothing Then Call objINET.Cancel End If End Sub Public Function DownloadFile(ByVal strRemoteFileURL As String, _ ByRef Return_Buffer() As Byte, _ Optional ByVal lngProtocol As Protocols = p_HTTP, _ Optional ByVal intPortNumber As Integer = 80, _ Optional ByVal lngTimeoutSeconds As Long = 60, _ Optional ByVal strProxyAddress As String, _ Optional ByVal strUserName As String, _ Optional ByVal strPassword As String, _ Optional ByRef Return_ResponseCode As Long, _ Optional ByRef Return_ResponseInfo As String, _ Optional ByRef Return_ErrNum As Long, _ Optional ByRef Return_ErrSrc As String, _ Optional ByRef Return_ErrDesc As String) As Boolean On Error GoTo ErrorTrap ' Set default values strRemoteFileURL = Trim(strRemoteFileURL) strProxyAddress = Trim(strProxyAddress) Erase Return_Buffer Return_ResponseCode = 0 Return_ResponseInfo = "" Return_ErrNum = 0 Return_ErrSrc = "" Return_ErrDesc = "" ' If there was a previous isntance running, close it If Not objINET Is Nothing Then objINET.Cancel Set objINET = Nothing End If ' Validate parameters If strRemoteFileURL = "" Then Return_ErrNum = -1: Return_ErrSrc = "DownloadFile()": Return_ErrDesc = "No remote file specified to download" Exit Function ElseIf lngProtocol <> p_Unknown And lngProtocol <> p_Default And lngProtocol <> p_FTP And lngProtocol <> p_Gopher And lngProtocol <> p_HTTP And lngProtocol <> p_HTTPS Then Return_ErrNum = -1: Return_ErrSrc = "DownloadFile()": Return_ErrDesc = "Invalid download protocol specified" Exit Function End If ' Create the INET object to work with Set objINET = New InetCtlsObjects.Inet 'Set objINET = CreateObject("InetCtls.Inet") ' Set the transfer parameters objINET.Protocol = lngProtocol objINET.RequestTimeout = lngTimeoutSeconds objINET.UserName = strUserName objINET.Password = strPassword objINET.URL = strRemoteFileURL objINET.RemotePort = intPortNumber ' Set the Access Type If Trim(strProxyAddress) = "" Then objINET.AccessType = InetCtlsObjects.AccessConstants.icUseDefault Else objINET.AccessType = InetCtlsObjects.AccessConstants.icNamedProxy objINET.Proxy = strProxyAddress End If ' Open a connection to the server and return the file in BINARY mode Return_Buffer() = objINET.OpenURL(, icByteArray) ' Wait for the process to complete Do While objINET.StillExecuting = True DoEvents Loop ' Return the information that was returned from the request Return_ResponseCode = objINET.ResponseCode Return_ResponseInfo = objINET.ResponseInfo ' Clean up Set objINET = Nothing DownloadFile = True Exit Function ErrorTrap: Return_ErrNum = Err.Number Return_ErrSrc = Err.Source Return_ErrDesc = Err.Description Err.Clear Erase Return_Buffer Return_ResponseCode = objINET.ResponseCode Return_ResponseInfo = objINET.ResponseInfo Set objINET = Nothing End Function