用Microsoft Internet Transfer Control 实现 FTP

来源:互联网 发布:云计算需要的设备 编辑:程序博客网 时间:2024/04/29 17:53

'需引用 Microsoft Internet Transfer Control
'定位于 C:/Windows/System32/MSINET.OCX

 

Private bInetCode As Boolean

'Inet 延迟等待
Private Sub Inet_Wait()
    While Inet.StillExecuting
        DoEvents
    Wend
End Sub

 

Private Sub FtpFile(ByRef Host As String, ByRef SourceFile As String, ByRef TragetFile As String)
    Dim TempFile As String
    TempFile = "./ls.txt"

    With Inet
'        .RemoteHost = ""
        .RemotePort = 21
        .AccessType = icDirect
        .Protocol = icFTP
'        .RequestTimeout = 1000
'        .UserName = ""
'        .Password = ""
    End With

    Inet.Execute Host, "SIZE " & SourceFile
    Inet_Wait
    If Not bInetCode Then GoTo CancelKey

    Inet.Execute Host, "GET " & SourceFile & " " & TempFile
    Inet_Wait
   
    Inet.Execute Host, "QUIT"
    Inet_Wait
   
    Kill (TargetFile)
    Name TempFile As TargetFile
CancelKey:
End Sub

 

Private Sub Inet_StateChanged(ByVal State As Integer)
    Dim vtData As Variant
    Dim strInetMsg(12) As String
   
    '### StateConstants
    strInetMsg(0) = "未报告状态"    'icNone
    strInetMsg(1) = "控件正在寻找指定主机的IP地址"  'icResolvingHost
    strInetMsg(2) = "控件已成功找到指定主机的IP地址"    'icHostResolved
    strInetMsg(3) = "控件正在与指定主机进行连接"    'icConnecting
    strInetMsg(4) = "控件已成功与指定主机连接"  'icConnected
    strInetMsg(5) = "控件正在向主机发出请求"    'icRequesting
    strInetMsg(6) = "控件已成功向主机发出请求"  'icRequestSent
    strInetMsg(7) = "控件正在从主机接收反馈信息"    'icReceivingResponse
    strInetMsg(8) = "控件已成功从主机接受反馈信息"  'icResponseReceived
    strInetMsg(9) = "控件正在与主机断开"    'icDisconnecting
    strInetMsg(10) = "控件已与主机断开" 'icDisconnected
    strInetMsg(11) = "在与主机通信的过程中发生了错误"   'icError
    strInetMsg(12) = "请求结束且数据已经接收到" 'icResponseCompleted

   
    bInetCode = True
   
    Debug.Print strInetMsg(State) & " CODE= " & State
     If State = icError Then
        '发生icError后查看ResponseCode和ResponseInfo,以查找错误
        Debug.Print "ResponseCode=" & Inet.ResponseCode & "|ResponseInfo=" & Inet.ResponseInfo
        If Inet.ResponseCode = 18 Then
            Debug.Print "文件不存在"
            bInetCode = False
        End If
     ElseIf State = icResponseCompleted Then
            vtData = Inet1.GetChunk(1024, icString)
            If vtData = 0 Then
                Debug.Print "文件不存在或文件长度为零!"
                bInetCode = False
            End If
    End If
End Sub

 

'FTP 命令:
'CD file1
'    改变目录。改变到由 file1 指定的目录中
'    Execute,"CD docs/mydocs"
'CDUP
'    改变到父目录。功能与 "CD .." 相同
'    Execute , "CDUP"
'DELETE file1
'    删除由 file1 指定的文件
'    Execute,"DELETE discard.txt"
'DIR [file1]
'    在由file1指定的目录中查找。如果没有指定file1目录,则查找当前工作目录。使用GetChunk方法返回数据
'    Execute , "DIR /mydocs"
'GET file1 file2
'    获取由 file1 指定的远程文件,并创建由 file2 指定的新的本地文件
'    Execute, "GET getme.txt C:/gotme.txt"
'MKDIR file1
'    创建由 file1 指定的目录。是否能够成功地执行,取决于用户在远程主机上的权限
'    Execute , "MKDIR /myDir"
'PUT file1 file2
'    将由file1指定的本地文件,复制到由 file2 指定的远程主机文件中
'    Execute, "PUT C:/putme.txt /putme.txt"
'PWD
'    打印工作目录。返回当前目录的名称。用 GetChunk 方法返回数据
'    Execute , "PWD"
'QUIT
'    结束当前连接
'    Execute , "QUIT"
'RECV file1 file2
'    与GET相同
'    Execute , "RECV getme.txt C:/gotme.txt"
'RENAME file1 file2
'    文件重命名。是否能够成功地执行,取决于用户在远程主机上的权限
'    Execute , "RENAME old.txt new.txt"
'RMDIR file1
'    删除目录。是否能够成功地执行,取决于用户在远程主机上的权限
'    Execute , "RMDIR oldDir"
'SEND file1
'    将文件复制到 FTP 站点。(与PUT相同)
'    Execute,_"SEND C:/putme.txt /putme.txt"
'SIZE file1
'    返回由 file1 指定文件的大小
'    Execute "SIZE /largefile.txt"


'在HTTP协议上使用Execute方法
'GET
'    获取 url 中命名的文件
'    Execute "http://www.microsoft.com" & "/default.htm", "GET"
'HEAD
'    只获取 URL 属性中命名的文件的文件标头
'    Execute , "HEAD"
'POST
'    提供附加数据,以支持向远程主机的请求
'    Execute , "POST", strFormData
'PUT
'    替换指定的URL中的数据
'    Execute , "PUT", "replace.htm"

原创粉丝点击