none
使用Inet控制項寫的Ftp程式,如何設定Firewall的屬性 RRS feed

  • 問題

  • 在VB6使用Inet控制項寫的Ftp程式,

    該如何設定Proxy的Host,帳號與密碼?

    由於公司對外有設firewall, 所以造成我寫的ftp程式一直無法連線到ftp主機,

    但是如果使用ie去下ftp命令,

    卻可以出現ftp內容

    希望知道此問題處理方式的高手可以交授解決方法

    謝謝

     

    2007年3月6日 上午 07:23

解答

  • 感謝 璉璉 的幫助, 針對這個問題我已經完全解決了, 所以今天我把我的FTP程式上傳與大家分享,

    (PS:因為是擷取部份別人的程式,所以有可能有多餘的宣告)

    以下是我的 VB 程式 :

    ================================================================

    Attribute VB_Name = "FTPModule"
    Option Explicit

    Declare Function GetProcessHeap Lib "kernel32" () As Long
    Declare Function HeapAlloc Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
    Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, lpMem As Any) As Long
    Public Const HEAP_ZERO_MEMORY = &H8
    Public Const HEAP_GENERATE_EXCEPTIONS = &H4

    Declare Sub CopyMemory1 Lib "kernel32" Alias "RtlMoveMemory" ( _
             hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
    Declare Sub CopyMemory2 Lib "kernel32" Alias "RtlMoveMemory" ( _
             hpvDest As Long, hpvSource As Any, ByVal cbCopy As Long)

    Public Const MAX_PATH = 260
    Public Const NO_ERROR = 0
    Public Const FILE_ATTRIBUTE_READONLY = &H1
    Public Const FILE_ATTRIBUTE_HIDDEN = &H2
    Public Const FILE_ATTRIBUTE_SYSTEM = &H4
    Public Const FILE_ATTRIBUTE_DIRECTORY = &H10
    Public Const FILE_ATTRIBUTE_ARCHIVE = &H20
    Public Const FILE_ATTRIBUTE_NORMAL = &H80
    Public Const FILE_ATTRIBUTE_TEMPORARY = &H100
    Public Const FILE_ATTRIBUTE_COMPRESSED = &H800
    Public Const FILE_ATTRIBUTE_OFFLINE = &H1000


    Type FILETIME
            dwLowDateTime As Long
            dwHighDateTime As Long
    End Type

    Type WIN32_FIND_DATA
            dwFileAttributes As Long
            ftCreationTime As FILETIME
            ftLastAccessTime As FILETIME
            ftLastWriteTime As FILETIME
            nFileSizeHigh As Long
            nFileSizeLow As Long
            dwReserved0 As Long
            dwReserved1 As Long
            cFileName As String * MAX_PATH
            cAlternate As String * 14
    End Type


    Public Const ERROR_NO_MORE_FILES = 18

    Public Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" _
        (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long
       
    Public Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" _
    (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, _
          lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long

    Public Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" _
    (ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, _
          ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttributes As Long, _
          ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean

    Public Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" _
    (ByVal hFtpSession As Long, ByVal lpszLocalFile As String, _
          ByVal lpszRemoteFile As String, _
          ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean

    Public Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" _
        (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
    ' Initializes an application's use of the Win32 Internet functions
    Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
    (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, _
    ByVal sProxyBypass As String, ByVal lflags As Long) As Long

    ' User agent constant.
    Public Const scUserAgent = "vb wininet"

    ' Use registry access settings.
    Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0
    Public Const INTERNET_OPEN_TYPE_DIRECT = 1
    Public Const INTERNET_OPEN_TYPE_PROXY = 3
    Public Const INTERNET_INVALID_PORT_NUMBER = 0

    Public Const FTP_TRANSFER_TYPE_BINARY = &H2
    Public Const FTP_TRANSFER_TYPE_ASCII = &H1
    Public Const INTERNET_FLAG_PASSIVE = &H8000000

    ' Opens a HTTP session for a given site.
    Public Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" _
    (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, _
    ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Long, _
    ByVal lflags As Long, ByVal lContext As Long) As Long

    ' Use FTP Command   2007-03-21 Peter
    Public Declare Function FtpCommand _
    Lib "wininet.dll" Alias "FtpCommandA" (ByVal hConnect As Long, _
                                          ByVal fExpectResponse As Boolean, _
                                          ByVal dwFlags As Long, _
                                          ByVal lpszCommand As String, _
                                          ByVal lContext As Long, _
                                          phFtpCommand As Long) As Boolean
     

                   
    Public Const ERROR_INTERNET_EXTENDED_ERROR = 12003
    Public Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" ( _
        lpdwError As Long, _
        ByVal lpszBuffer As String, _
        lpdwBufferLength As Long) As Boolean

    ' Number of the TCP/IP port on the server to connect to.
    Public Const INTERNET_DEFAULT_FTP_PORT = 21
    Public Const INTERNET_DEFAULT_GOPHER_PORT = 70
    Public Const INTERNET_DEFAULT_HTTP_PORT = 80
    Public Const INTERNET_DEFAULT_HTTPS_PORT = 443
    Public Const INTERNET_DEFAULT_SOCKS_PORT = 1080

    Public Const INTERNET_OPTION_CONNECT_TIMEOUT = 2
    Public Const INTERNET_OPTION_RECEIVE_TIMEOUT = 6
    Public Const INTERNET_OPTION_SEND_TIMEOUT = 5

    Public Const INTERNET_OPTION_USERNAME = 28
    Public Const INTERNET_OPTION_PASSWORD = 29
    Public Const INTERNET_OPTION_PROXY_USERNAME = 43
    Public Const INTERNET_OPTION_PROXY_PASSWORD = 44

    ' Type of service to access.
    Public Const INTERNET_SERVICE_FTP = 1
    Public Const INTERNET_SERVICE_GOPHER = 2
    Public Const INTERNET_SERVICE_HTTP = 3

    ' Opens an HTTP request handle.
    Public Declare Function HttpOpenRequest Lib "wininet.dll" Alias "HttpOpenRequestA" _
    (ByVal hHttpSession As Long, ByVal sVerb As String, ByVal sObjectName As String, ByVal sVersion As String, _
    ByVal sReferer As String, ByVal something As Long, ByVal lflags As Long, ByVal lContext As Long) As Long

    ' Brings the data across the wire even if it locally cached.
    Public Const INTERNET_FLAG_RELOAD = &H80000000
    Public Const INTERNET_FLAG_KEEP_CONNECTION = &H400000
    Public Const INTERNET_FLAG_MULTIPART = &H200000

    Public Const GENERIC_READ = &H80000000
    Public Const GENERIC_WRITE = &H40000000

    ' Sends the specified request to the HTTP server.
    Public Declare Function HttpSendRequest Lib "wininet.dll" Alias "HttpSendRequestA" (ByVal _
    hHttpRequest As Long, ByVal sHeaders As String, ByVal lHeadersLength As Long, ByVal sOptional As _
    String, ByVal lOptionalLength As Long) As Integer


    ' Queries for information about an HTTP request.
    Public Declare Function HttpQueryInfo Lib "wininet.dll" Alias "HttpQueryInfoA" _
    (ByVal hHttpRequest As Long, ByVal lInfoLevel As Long, ByRef sBuffer As Any, _
    ByRef lBufferLength As Long, ByRef lIndex As Long) As Integer

    ' The possible values for the lInfoLevel parameter include:
    Public Const HTTP_QUERY_CONTENT_TYPE = 1
    Public Const HTTP_QUERY_CONTENT_LENGTH = 5
    Public Const HTTP_QUERY_EXPIRES = 10
    Public Const HTTP_QUERY_LAST_MODIFIED = 11
    Public Const HTTP_QUERY_PRAGMA = 17
    Public Const HTTP_QUERY_VERSION = 18
    Public Const HTTP_QUERY_STATUS_CODE = 19
    Public Const HTTP_QUERY_STATUS_TEXT = 20
    Public Const HTTP_QUERY_RAW_HEADERS = 21
    Public Const HTTP_QUERY_RAW_HEADERS_CRLF = 22
    Public Const HTTP_QUERY_FORWARDED = 30
    Public Const HTTP_QUERY_SERVER = 37
    Public Const HTTP_QUERY_USER_AGENT = 39
    Public Const HTTP_QUERY_SET_COOKIE = 43
    Public Const HTTP_QUERY_REQUEST_METHOD = 45
    Public Const HTTP_STATUS_DENIED = 401
    Public Const HTTP_STATUS_PROXY_AUTH_REQ = 407

    ' Add this flag to the about flags to get request header.
    Public Const HTTP_QUERY_FLAG_REQUEST_HEADERS = &H80000000
    Public Const HTTP_QUERY_FLAG_NUMBER = &H20000000
    ' Reads data from a handle opened by the HttpOpenRequest function.
    Public Declare Function InternetReadFile Lib "wininet.dll" _
    (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, _
    lNumberOfBytesRead As Long) As Integer

    Public Declare Function InternetWriteFile Lib "wininet.dll" _
            (ByVal hFile As Long, ByVal sBuffer As String, _
            ByVal lNumberOfBytesToRead As Long, _
            lNumberOfBytesRead As Long) As Integer

    Public Declare Function FtpOpenFile Lib "wininet.dll" Alias _
            "FtpOpenFileA" (ByVal hFtpSession As Long, _
            ByVal sFileName As String, ByVal lAccess As Long, _
            ByVal lflags As Long, ByVal lContext As Long) As Long
    Public Declare Function FtpDeleteFile Lib "wininet.dll" _
        Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, _
        ByVal lpszFileName As String) As Boolean
    Public Declare Function InternetSetOption Lib "wininet.dll" Alias "InternetSetOptionA" _
    (ByVal hInternet As Long, ByVal lOption As Long, ByRef sBuffer As Any, ByVal lBufferLength As Long) As Integer
    Public Declare Function InternetSetOptionStr Lib "wininet.dll" Alias "InternetSetOptionA" _
    (ByVal hInternet As Long, ByVal lOption As Long, ByVal sBuffer As String, ByVal lBufferLength As Long) As Integer

    ' Closes a single Internet handle or a subtree of Internet handles.
    Public Declare Function InternetCloseHandle Lib "wininet.dll" _
    (ByVal hInet As Long) As Integer

    ' Queries an Internet option on the specified handle
    Public Declare Function InternetQueryOption Lib "wininet.dll" Alias "InternetQueryOptionA" _
    (ByVal hInternet As Long, ByVal lOption As Long, ByRef sBuffer As Any, ByRef lBufferLength As Long) As Integer

    ' Returns the version number of Wininet.dll.
    Public Const INTERNET_OPTION_VERSION = 40

    ' Contains the version number of the DLL that contains the Windows Internet
    ' functions (Wininet.dll). This structure is used when passing the
    ' INTERNET_OPTION_VERSION flag to the InternetQueryOption function.
    Public Type tWinInetDLLVersion
        lMajorVersion As Long
        lMinorVersion As Long
    End Type

    ' Adds one or more HTTP request headers to the HTTP request handle.
    Public Declare Function HttpAddRequestHeaders Lib "wininet.dll" Alias "HttpAddRequestHeadersA" _
    (ByVal hHttpRequest As Long, ByVal sHeaders As String, ByVal lHeadersLength As Long, _
    ByVal lModifiers As Long) As Integer

    ' Flags to modify the semantics of this function. Can be a combination of these values:

    ' Adds the header only if it does not already exist; otherwise, an error is returned.
    Public Const HTTP_ADDREQ_FLAG_ADD_IF_NEW = &H10000000

    ' Adds the header if it does not exist. Used with REPLACE.
    Public Const HTTP_ADDREQ_FLAG_ADD = &H20000000

    ' Replaces or removes a header. If the header value is empty and the header is found,
    ' it is removed. If not empty, the header value is replaced
    Public Const HTTP_ADDREQ_FLAG_REPLACE = &H80000000


    'Proxy Setting
    Public Const PROXY_NAME = <Proxy IP>
    Public Const PROXY_USER_NAME = <Proxy 驗證帳號>
    Public Const PROXY_USER_PASSWORD = <Proxy 驗證密碼>

    '------------------------------------------------------
    '目的:上傳檔案到FTP
    '------------------------------------------------------
    'FTPServer  : FTP Server IP
    'UserName   : FTP 登入帳號
    'UserPW     : FTP 登入密碼
    'CDir       : 登入後切換到某個目錄
    'localPath  : Local路徑
    'localFile  : 上傳檔案
    '------------------------------------------------------
    Public Function PutFTP(ByVal FTPServer As String, ByVal UserName As String, ByVal UserPW As String, _
                            ByVal CDir As String, ByVal localPath As String, ByVal localFile As String) As Boolean
        Dim hOpen As Long, hConnection As Long
        Dim vtData As Variant
        Dim pData As WIN32_FIND_DATA
        Dim bRet As Boolean
        Dim ln_findok As Long, ln_findnext As Long
        Dim lFile As String
        Dim i As Integer
       
        On Error GoTo Err1
       
        PutFTP = False
       
    '-------------------------------------

        hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PROXY, PROXY_NAME, vbNullString, 0)
        If hOpen = 0 Then
          MsgBox "Internet Open Fails."
          Exit Function
        End If
       
        hConnection = InternetConnect(hOpen, PROXY_NAME, INTERNET_INVALID_PORT_NUMBER, _
            PROXY_USER_NAME, PROXY_USER_PASSWORD, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0)
       
        If hConnection = 0 Then
          InternetCloseHandle (hOpen)
          MsgBox "Proxy Connection Fails."
          Exit Function
        Else
            FtpCommand hConnection, 0, 0, "user " & UserName & "@" & FTPServer, 0, 1     '連結到FTP
            FtpCommand hConnection, 0, 0, "pass " & UserPW, 0, 1                         '輸入密碼
            If CDir <> "" Then FtpCommand hConnection, 0, 0, "CWD " & CDir, 0, 1         '切換目錄
           
            lFile = localPath & "\" & localFile
           
            bRet = FtpPutFile(hConnection, lFile, localFile, 0, 0)                       '上傳檔案
            If bRet = False Then
              '重新嘗試五次,如果還是失敗,則回傳錯誤訊息
              For i = 0 To 5
                bRet = FtpPutFile(hConnection, lFile, localFile, FTP_TRANSFER_TYPE_BINARY, 0)
                If bRet = True Then Exit For
              Next i
              If bRet = False Then
                InternetCloseHandle (hConnection)
                InternetCloseHandle (hOpen)
                MsgBox "Upload Files Fails!"
                Exit Function
              End If
            End If
        End If
        InternetCloseHandle (hConnection)
        InternetCloseHandle (hOpen)
       
    '-------------------------------------

        PutFTP = True
       
        Exit Function
    Err1:
      MsgBox Err.Description
    '  Resume Next
    End Function

    '------------------------------------------------------
    '目的:特定目錄下的檔案下載,下載完會自動刪除FTP上的檔案
    '------------------------------------------------------
    'FTPServer  : FTP Server IP
    'UserName   : FTP 登入帳號
    'UserPW     : FTP 登入密碼
    'CDir       : 登入後切換到某個目錄
    'dwFileType : 下載檔案類型
    'localPath  : Local存放位置
    '------------------------------------------------------
    Public Function GetFTP(ByVal FTPServer As String, ByVal UserName As String, ByVal UserPW As String, _
                              ByVal CDir As String, ByVal dwFileType As String, ByVal localPath As String) As Boolean
        Dim hOpen As Long, hConnection As Long
        Dim vtData As Variant
        Dim pData As WIN32_FIND_DATA
        Dim bRet As Boolean
        Dim ln_findok As Long, ln_findnext As Long
        Dim lFile As String
        Dim i As Integer
       
        On Error GoTo Err1
       
        GetFTP = False
    '-------------------------------------
       
        hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PROXY, PROXY_NAME, vbNullString, 0)
        If hOpen = 0 Then
          MsgBox "Internet Open Fails."
          Exit Function
        End If
       
        hConnection = InternetConnect(hOpen, PROXY_NAME, INTERNET_INVALID_PORT_NUMBER, _
            PROXY_USER_NAME, PROXY_USER_PASSWORD, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0)
       
        If hConnection = 0 Then
          InternetCloseHandle (hOpen)
          MsgBox "Proxy Connection Fails."
          Exit Function
        Else
            FtpCommand hConnection, 0, 0, "user " & UserName & "@" & FTPServer, 0, 1     '連結到FTP
            FtpCommand hConnection, 0, 0, "pass " & UserPW, 0, 1                         '輸入密碼
            If CDir <> "" Then FtpCommand hConnection, 0, 0, "CWD " & CDir, 0, 1         '切換目錄
                   
            pData.cFileName = String(MAX_PATH, 0)
            ln_findok = FtpFindFirstFile(hConnection, dwFileType, pData, 0, 1)           '取得第一個檔案
            If ln_findok > 0 Then
              lFile = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)   '取出檔名
              If pData.dwFileAttributes <> FILE_ATTRIBUTE_DIRECTORY Then
                bRet = FtpGetFile(hConnection, lFile, localPath & lFile, 0, 0, 0, 0)     '下載檔案
                If bRet = False Then
                  '重新嘗試五次,如果還是失敗,則回傳錯誤訊息
                  For i = 0 To 5
                    bRet = FtpGetFile(hConnection, lFile, localPath & lFile, 0, 0, 0, 0)     '下載檔案
                    If bRet = True Then Exit For
                  Next i
                  If bRet = False Then
                    InternetCloseHandle (ln_findok)
                    InternetCloseHandle (hConnection)
                    InternetCloseHandle (hOpen)
                    MsgBox "Download Files Fails!"
                    Exit Function
                  End If
                End If
               
                bRet = FtpDeleteFile(hConnection, lFile)       '刪除檔案
                If bRet = False Then
                  '重新嘗試五次,如果還是失敗,則回傳錯誤訊息
                  For i = 0 To 5
                    bRet = FtpDeleteFile(hConnection, lFile)       '刪除檔案
                    If bRet = True Then Exit For
                  Next i
                  If bRet = False Then
                    InternetCloseHandle (ln_findok)
                    InternetCloseHandle (hConnection)
                    InternetCloseHandle (hOpen)
                    MsgBox "Delete Files Fails!"
                    Exit Function
                  End If
                End If

              End If
              ln_findnext = InternetFindNextFile(ln_findok, pData)                  '取得下個檔案
              Do While ln_findnext > 0
                lFile = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
                If pData.dwFileAttributes <> FILE_ATTRIBUTE_DIRECTORY Then
               
                  bRet = FtpGetFile(hConnection, lFile, localPath & lFile, 0, 0, 0, 0)
                  If bRet = False Then
                    '重新嘗試五次,如果還是失敗,則回傳錯誤訊息
                    For i = 0 To 5
                      bRet = FtpGetFile(hConnection, lFile, localPath & lFile, 0, 0, 0, 0)     '下載檔案
                      If bRet = True Then Exit For
                    Next i
                    If bRet = False Then
                      InternetCloseHandle (ln_findok)
                      InternetCloseHandle (hConnection)
                      InternetCloseHandle (hOpen)
                      MsgBox "Download Files Fails!"
                      Exit Function
                    End If
                  End If
                 
                  bRet = FtpDeleteFile(hConnection, lFile)       '刪除檔案
                  If bRet = False Then
                    '重新嘗試五次,如果還是失敗,則回傳錯誤訊息
                    For i = 0 To 5
                      bRet = FtpDeleteFile(hConnection, lFile)       '刪除檔案
                      If bRet = True Then Exit For
                    Next i
                    If bRet = False Then
                      InternetCloseHandle (ln_findok)
                      InternetCloseHandle (hConnection)
                      InternetCloseHandle (hOpen)
                      MsgBox "Delete Files Fails!"
                      Exit Function
                    End If
                  End If
                End If
                ln_findnext = InternetFindNextFile(ln_findok, pData)
              Loop
            End If
        End If
       
        InternetCloseHandle (ln_findok)
        InternetCloseHandle (hConnection)
        InternetCloseHandle (hOpen)
       
    '-------------------------------------

        GetFTP = True
       
        Exit Function
    Err1:
      MsgBox Err.Description
    '  Resume Next
    End Function

     

     

    2007年3月21日 上午 07:58

所有回覆

  • 在 IE6 之前,若是你的網際網路選項已經設定好了,則 Inet / wininet.dll 都會引用。

    在 IE7 會有問題。

    在 Inet 控制項內允許變更 proxy 設定,但不支援 proxy 驗證,這篇有說明該限制:

    http://support.microsoft.com/kb/233037/EN-US/

    2007年3月6日 上午 07:38
    版主
  •  璉璉 寫信:

    在 IE6 之前,若是你的網際網路選項已經設定好了,則 Inet / wininet.dll 都會引用。

    在 IE7 會有問題。

    在 Inet 控制項內允許變更 proxy 設定,但不支援 proxy 驗證,這篇有說明該限制:

    http://support.microsoft.com/kb/233037/EN-US/

     

    感謝 璉璉 與 Zion Wu 的回覆, 如果想克服這個問題, 是否有其他方法或是其他的元件可以代替Inet使用呢?

    2007年3月8日 下午 04:38
  • 感謝 璉璉 的幫助, 針對這個問題我已經完全解決了, 所以今天我把我的FTP程式上傳與大家分享,

    (PS:因為是擷取部份別人的程式,所以有可能有多餘的宣告)

    以下是我的 VB 程式 :

    ================================================================

    Attribute VB_Name = "FTPModule"
    Option Explicit

    Declare Function GetProcessHeap Lib "kernel32" () As Long
    Declare Function HeapAlloc Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
    Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, lpMem As Any) As Long
    Public Const HEAP_ZERO_MEMORY = &H8
    Public Const HEAP_GENERATE_EXCEPTIONS = &H4

    Declare Sub CopyMemory1 Lib "kernel32" Alias "RtlMoveMemory" ( _
             hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
    Declare Sub CopyMemory2 Lib "kernel32" Alias "RtlMoveMemory" ( _
             hpvDest As Long, hpvSource As Any, ByVal cbCopy As Long)

    Public Const MAX_PATH = 260
    Public Const NO_ERROR = 0
    Public Const FILE_ATTRIBUTE_READONLY = &H1
    Public Const FILE_ATTRIBUTE_HIDDEN = &H2
    Public Const FILE_ATTRIBUTE_SYSTEM = &H4
    Public Const FILE_ATTRIBUTE_DIRECTORY = &H10
    Public Const FILE_ATTRIBUTE_ARCHIVE = &H20
    Public Const FILE_ATTRIBUTE_NORMAL = &H80
    Public Const FILE_ATTRIBUTE_TEMPORARY = &H100
    Public Const FILE_ATTRIBUTE_COMPRESSED = &H800
    Public Const FILE_ATTRIBUTE_OFFLINE = &H1000


    Type FILETIME
            dwLowDateTime As Long
            dwHighDateTime As Long
    End Type

    Type WIN32_FIND_DATA
            dwFileAttributes As Long
            ftCreationTime As FILETIME
            ftLastAccessTime As FILETIME
            ftLastWriteTime As FILETIME
            nFileSizeHigh As Long
            nFileSizeLow As Long
            dwReserved0 As Long
            dwReserved1 As Long
            cFileName As String * MAX_PATH
            cAlternate As String * 14
    End Type


    Public Const ERROR_NO_MORE_FILES = 18

    Public Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" _
        (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long
       
    Public Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" _
    (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, _
          lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long

    Public Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" _
    (ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, _
          ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttributes As Long, _
          ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean

    Public Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" _
    (ByVal hFtpSession As Long, ByVal lpszLocalFile As String, _
          ByVal lpszRemoteFile As String, _
          ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean

    Public Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" _
        (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
    ' Initializes an application's use of the Win32 Internet functions
    Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
    (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, _
    ByVal sProxyBypass As String, ByVal lflags As Long) As Long

    ' User agent constant.
    Public Const scUserAgent = "vb wininet"

    ' Use registry access settings.
    Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0
    Public Const INTERNET_OPEN_TYPE_DIRECT = 1
    Public Const INTERNET_OPEN_TYPE_PROXY = 3
    Public Const INTERNET_INVALID_PORT_NUMBER = 0

    Public Const FTP_TRANSFER_TYPE_BINARY = &H2
    Public Const FTP_TRANSFER_TYPE_ASCII = &H1
    Public Const INTERNET_FLAG_PASSIVE = &H8000000

    ' Opens a HTTP session for a given site.
    Public Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" _
    (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, _
    ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Long, _
    ByVal lflags As Long, ByVal lContext As Long) As Long

    ' Use FTP Command   2007-03-21 Peter
    Public Declare Function FtpCommand _
    Lib "wininet.dll" Alias "FtpCommandA" (ByVal hConnect As Long, _
                                          ByVal fExpectResponse As Boolean, _
                                          ByVal dwFlags As Long, _
                                          ByVal lpszCommand As String, _
                                          ByVal lContext As Long, _
                                          phFtpCommand As Long) As Boolean
     

                   
    Public Const ERROR_INTERNET_EXTENDED_ERROR = 12003
    Public Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" ( _
        lpdwError As Long, _
        ByVal lpszBuffer As String, _
        lpdwBufferLength As Long) As Boolean

    ' Number of the TCP/IP port on the server to connect to.
    Public Const INTERNET_DEFAULT_FTP_PORT = 21
    Public Const INTERNET_DEFAULT_GOPHER_PORT = 70
    Public Const INTERNET_DEFAULT_HTTP_PORT = 80
    Public Const INTERNET_DEFAULT_HTTPS_PORT = 443
    Public Const INTERNET_DEFAULT_SOCKS_PORT = 1080

    Public Const INTERNET_OPTION_CONNECT_TIMEOUT = 2
    Public Const INTERNET_OPTION_RECEIVE_TIMEOUT = 6
    Public Const INTERNET_OPTION_SEND_TIMEOUT = 5

    Public Const INTERNET_OPTION_USERNAME = 28
    Public Const INTERNET_OPTION_PASSWORD = 29
    Public Const INTERNET_OPTION_PROXY_USERNAME = 43
    Public Const INTERNET_OPTION_PROXY_PASSWORD = 44

    ' Type of service to access.
    Public Const INTERNET_SERVICE_FTP = 1
    Public Const INTERNET_SERVICE_GOPHER = 2
    Public Const INTERNET_SERVICE_HTTP = 3

    ' Opens an HTTP request handle.
    Public Declare Function HttpOpenRequest Lib "wininet.dll" Alias "HttpOpenRequestA" _
    (ByVal hHttpSession As Long, ByVal sVerb As String, ByVal sObjectName As String, ByVal sVersion As String, _
    ByVal sReferer As String, ByVal something As Long, ByVal lflags As Long, ByVal lContext As Long) As Long

    ' Brings the data across the wire even if it locally cached.
    Public Const INTERNET_FLAG_RELOAD = &H80000000
    Public Const INTERNET_FLAG_KEEP_CONNECTION = &H400000
    Public Const INTERNET_FLAG_MULTIPART = &H200000

    Public Const GENERIC_READ = &H80000000
    Public Const GENERIC_WRITE = &H40000000

    ' Sends the specified request to the HTTP server.
    Public Declare Function HttpSendRequest Lib "wininet.dll" Alias "HttpSendRequestA" (ByVal _
    hHttpRequest As Long, ByVal sHeaders As String, ByVal lHeadersLength As Long, ByVal sOptional As _
    String, ByVal lOptionalLength As Long) As Integer


    ' Queries for information about an HTTP request.
    Public Declare Function HttpQueryInfo Lib "wininet.dll" Alias "HttpQueryInfoA" _
    (ByVal hHttpRequest As Long, ByVal lInfoLevel As Long, ByRef sBuffer As Any, _
    ByRef lBufferLength As Long, ByRef lIndex As Long) As Integer

    ' The possible values for the lInfoLevel parameter include:
    Public Const HTTP_QUERY_CONTENT_TYPE = 1
    Public Const HTTP_QUERY_CONTENT_LENGTH = 5
    Public Const HTTP_QUERY_EXPIRES = 10
    Public Const HTTP_QUERY_LAST_MODIFIED = 11
    Public Const HTTP_QUERY_PRAGMA = 17
    Public Const HTTP_QUERY_VERSION = 18
    Public Const HTTP_QUERY_STATUS_CODE = 19
    Public Const HTTP_QUERY_STATUS_TEXT = 20
    Public Const HTTP_QUERY_RAW_HEADERS = 21
    Public Const HTTP_QUERY_RAW_HEADERS_CRLF = 22
    Public Const HTTP_QUERY_FORWARDED = 30
    Public Const HTTP_QUERY_SERVER = 37
    Public Const HTTP_QUERY_USER_AGENT = 39
    Public Const HTTP_QUERY_SET_COOKIE = 43
    Public Const HTTP_QUERY_REQUEST_METHOD = 45
    Public Const HTTP_STATUS_DENIED = 401
    Public Const HTTP_STATUS_PROXY_AUTH_REQ = 407

    ' Add this flag to the about flags to get request header.
    Public Const HTTP_QUERY_FLAG_REQUEST_HEADERS = &H80000000
    Public Const HTTP_QUERY_FLAG_NUMBER = &H20000000
    ' Reads data from a handle opened by the HttpOpenRequest function.
    Public Declare Function InternetReadFile Lib "wininet.dll" _
    (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, _
    lNumberOfBytesRead As Long) As Integer

    Public Declare Function InternetWriteFile Lib "wininet.dll" _
            (ByVal hFile As Long, ByVal sBuffer As String, _
            ByVal lNumberOfBytesToRead As Long, _
            lNumberOfBytesRead As Long) As Integer

    Public Declare Function FtpOpenFile Lib "wininet.dll" Alias _
            "FtpOpenFileA" (ByVal hFtpSession As Long, _
            ByVal sFileName As String, ByVal lAccess As Long, _
            ByVal lflags As Long, ByVal lContext As Long) As Long
    Public Declare Function FtpDeleteFile Lib "wininet.dll" _
        Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, _
        ByVal lpszFileName As String) As Boolean
    Public Declare Function InternetSetOption Lib "wininet.dll" Alias "InternetSetOptionA" _
    (ByVal hInternet As Long, ByVal lOption As Long, ByRef sBuffer As Any, ByVal lBufferLength As Long) As Integer
    Public Declare Function InternetSetOptionStr Lib "wininet.dll" Alias "InternetSetOptionA" _
    (ByVal hInternet As Long, ByVal lOption As Long, ByVal sBuffer As String, ByVal lBufferLength As Long) As Integer

    ' Closes a single Internet handle or a subtree of Internet handles.
    Public Declare Function InternetCloseHandle Lib "wininet.dll" _
    (ByVal hInet As Long) As Integer

    ' Queries an Internet option on the specified handle
    Public Declare Function InternetQueryOption Lib "wininet.dll" Alias "InternetQueryOptionA" _
    (ByVal hInternet As Long, ByVal lOption As Long, ByRef sBuffer As Any, ByRef lBufferLength As Long) As Integer

    ' Returns the version number of Wininet.dll.
    Public Const INTERNET_OPTION_VERSION = 40

    ' Contains the version number of the DLL that contains the Windows Internet
    ' functions (Wininet.dll). This structure is used when passing the
    ' INTERNET_OPTION_VERSION flag to the InternetQueryOption function.
    Public Type tWinInetDLLVersion
        lMajorVersion As Long
        lMinorVersion As Long
    End Type

    ' Adds one or more HTTP request headers to the HTTP request handle.
    Public Declare Function HttpAddRequestHeaders Lib "wininet.dll" Alias "HttpAddRequestHeadersA" _
    (ByVal hHttpRequest As Long, ByVal sHeaders As String, ByVal lHeadersLength As Long, _
    ByVal lModifiers As Long) As Integer

    ' Flags to modify the semantics of this function. Can be a combination of these values:

    ' Adds the header only if it does not already exist; otherwise, an error is returned.
    Public Const HTTP_ADDREQ_FLAG_ADD_IF_NEW = &H10000000

    ' Adds the header if it does not exist. Used with REPLACE.
    Public Const HTTP_ADDREQ_FLAG_ADD = &H20000000

    ' Replaces or removes a header. If the header value is empty and the header is found,
    ' it is removed. If not empty, the header value is replaced
    Public Const HTTP_ADDREQ_FLAG_REPLACE = &H80000000


    'Proxy Setting
    Public Const PROXY_NAME = <Proxy IP>
    Public Const PROXY_USER_NAME = <Proxy 驗證帳號>
    Public Const PROXY_USER_PASSWORD = <Proxy 驗證密碼>

    '------------------------------------------------------
    '目的:上傳檔案到FTP
    '------------------------------------------------------
    'FTPServer  : FTP Server IP
    'UserName   : FTP 登入帳號
    'UserPW     : FTP 登入密碼
    'CDir       : 登入後切換到某個目錄
    'localPath  : Local路徑
    'localFile  : 上傳檔案
    '------------------------------------------------------
    Public Function PutFTP(ByVal FTPServer As String, ByVal UserName As String, ByVal UserPW As String, _
                            ByVal CDir As String, ByVal localPath As String, ByVal localFile As String) As Boolean
        Dim hOpen As Long, hConnection As Long
        Dim vtData As Variant
        Dim pData As WIN32_FIND_DATA
        Dim bRet As Boolean
        Dim ln_findok As Long, ln_findnext As Long
        Dim lFile As String
        Dim i As Integer
       
        On Error GoTo Err1
       
        PutFTP = False
       
    '-------------------------------------

        hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PROXY, PROXY_NAME, vbNullString, 0)
        If hOpen = 0 Then
          MsgBox "Internet Open Fails."
          Exit Function
        End If
       
        hConnection = InternetConnect(hOpen, PROXY_NAME, INTERNET_INVALID_PORT_NUMBER, _
            PROXY_USER_NAME, PROXY_USER_PASSWORD, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0)
       
        If hConnection = 0 Then
          InternetCloseHandle (hOpen)
          MsgBox "Proxy Connection Fails."
          Exit Function
        Else
            FtpCommand hConnection, 0, 0, "user " & UserName & "@" & FTPServer, 0, 1     '連結到FTP
            FtpCommand hConnection, 0, 0, "pass " & UserPW, 0, 1                         '輸入密碼
            If CDir <> "" Then FtpCommand hConnection, 0, 0, "CWD " & CDir, 0, 1         '切換目錄
           
            lFile = localPath & "\" & localFile
           
            bRet = FtpPutFile(hConnection, lFile, localFile, 0, 0)                       '上傳檔案
            If bRet = False Then
              '重新嘗試五次,如果還是失敗,則回傳錯誤訊息
              For i = 0 To 5
                bRet = FtpPutFile(hConnection, lFile, localFile, FTP_TRANSFER_TYPE_BINARY, 0)
                If bRet = True Then Exit For
              Next i
              If bRet = False Then
                InternetCloseHandle (hConnection)
                InternetCloseHandle (hOpen)
                MsgBox "Upload Files Fails!"
                Exit Function
              End If
            End If
        End If
        InternetCloseHandle (hConnection)
        InternetCloseHandle (hOpen)
       
    '-------------------------------------

        PutFTP = True
       
        Exit Function
    Err1:
      MsgBox Err.Description
    '  Resume Next
    End Function

    '------------------------------------------------------
    '目的:特定目錄下的檔案下載,下載完會自動刪除FTP上的檔案
    '------------------------------------------------------
    'FTPServer  : FTP Server IP
    'UserName   : FTP 登入帳號
    'UserPW     : FTP 登入密碼
    'CDir       : 登入後切換到某個目錄
    'dwFileType : 下載檔案類型
    'localPath  : Local存放位置
    '------------------------------------------------------
    Public Function GetFTP(ByVal FTPServer As String, ByVal UserName As String, ByVal UserPW As String, _
                              ByVal CDir As String, ByVal dwFileType As String, ByVal localPath As String) As Boolean
        Dim hOpen As Long, hConnection As Long
        Dim vtData As Variant
        Dim pData As WIN32_FIND_DATA
        Dim bRet As Boolean
        Dim ln_findok As Long, ln_findnext As Long
        Dim lFile As String
        Dim i As Integer
       
        On Error GoTo Err1
       
        GetFTP = False
    '-------------------------------------
       
        hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PROXY, PROXY_NAME, vbNullString, 0)
        If hOpen = 0 Then
          MsgBox "Internet Open Fails."
          Exit Function
        End If
       
        hConnection = InternetConnect(hOpen, PROXY_NAME, INTERNET_INVALID_PORT_NUMBER, _
            PROXY_USER_NAME, PROXY_USER_PASSWORD, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0)
       
        If hConnection = 0 Then
          InternetCloseHandle (hOpen)
          MsgBox "Proxy Connection Fails."
          Exit Function
        Else
            FtpCommand hConnection, 0, 0, "user " & UserName & "@" & FTPServer, 0, 1     '連結到FTP
            FtpCommand hConnection, 0, 0, "pass " & UserPW, 0, 1                         '輸入密碼
            If CDir <> "" Then FtpCommand hConnection, 0, 0, "CWD " & CDir, 0, 1         '切換目錄
                   
            pData.cFileName = String(MAX_PATH, 0)
            ln_findok = FtpFindFirstFile(hConnection, dwFileType, pData, 0, 1)           '取得第一個檔案
            If ln_findok > 0 Then
              lFile = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)   '取出檔名
              If pData.dwFileAttributes <> FILE_ATTRIBUTE_DIRECTORY Then
                bRet = FtpGetFile(hConnection, lFile, localPath & lFile, 0, 0, 0, 0)     '下載檔案
                If bRet = False Then
                  '重新嘗試五次,如果還是失敗,則回傳錯誤訊息
                  For i = 0 To 5
                    bRet = FtpGetFile(hConnection, lFile, localPath & lFile, 0, 0, 0, 0)     '下載檔案
                    If bRet = True Then Exit For
                  Next i
                  If bRet = False Then
                    InternetCloseHandle (ln_findok)
                    InternetCloseHandle (hConnection)
                    InternetCloseHandle (hOpen)
                    MsgBox "Download Files Fails!"
                    Exit Function
                  End If
                End If
               
                bRet = FtpDeleteFile(hConnection, lFile)       '刪除檔案
                If bRet = False Then
                  '重新嘗試五次,如果還是失敗,則回傳錯誤訊息
                  For i = 0 To 5
                    bRet = FtpDeleteFile(hConnection, lFile)       '刪除檔案
                    If bRet = True Then Exit For
                  Next i
                  If bRet = False Then
                    InternetCloseHandle (ln_findok)
                    InternetCloseHandle (hConnection)
                    InternetCloseHandle (hOpen)
                    MsgBox "Delete Files Fails!"
                    Exit Function
                  End If
                End If

              End If
              ln_findnext = InternetFindNextFile(ln_findok, pData)                  '取得下個檔案
              Do While ln_findnext > 0
                lFile = Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
                If pData.dwFileAttributes <> FILE_ATTRIBUTE_DIRECTORY Then
               
                  bRet = FtpGetFile(hConnection, lFile, localPath & lFile, 0, 0, 0, 0)
                  If bRet = False Then
                    '重新嘗試五次,如果還是失敗,則回傳錯誤訊息
                    For i = 0 To 5
                      bRet = FtpGetFile(hConnection, lFile, localPath & lFile, 0, 0, 0, 0)     '下載檔案
                      If bRet = True Then Exit For
                    Next i
                    If bRet = False Then
                      InternetCloseHandle (ln_findok)
                      InternetCloseHandle (hConnection)
                      InternetCloseHandle (hOpen)
                      MsgBox "Download Files Fails!"
                      Exit Function
                    End If
                  End If
                 
                  bRet = FtpDeleteFile(hConnection, lFile)       '刪除檔案
                  If bRet = False Then
                    '重新嘗試五次,如果還是失敗,則回傳錯誤訊息
                    For i = 0 To 5
                      bRet = FtpDeleteFile(hConnection, lFile)       '刪除檔案
                      If bRet = True Then Exit For
                    Next i
                    If bRet = False Then
                      InternetCloseHandle (ln_findok)
                      InternetCloseHandle (hConnection)
                      InternetCloseHandle (hOpen)
                      MsgBox "Delete Files Fails!"
                      Exit Function
                    End If
                  End If
                End If
                ln_findnext = InternetFindNextFile(ln_findok, pData)
              Loop
            End If
        End If
       
        InternetCloseHandle (ln_findok)
        InternetCloseHandle (hConnection)
        InternetCloseHandle (hOpen)
       
    '-------------------------------------

        GetFTP = True
       
        Exit Function
    Err1:
      MsgBox Err.Description
    '  Resume Next
    End Function

     

     

    2007年3月21日 上午 07:58
  • 我看過許多文章, 說

    1. Cern-based Proxy 是不能再用Wininet的API,

    不懂為何你還是可以成功使用FtpGetFile與FtpPutFile

    這是否也代表InternetReadFile與InternetWriteFile函式也可在你的程式中替代使用 ??

    在變更目錄時, 你是使用Ftpcommand, 可否使用FtpSetCurrentDirectory??

    2. 你的Ftp Client和Proxy之間是用FTP或HTTP溝通 ??

    我看到的是你使用INTERNET_SERVICE_FTP

    hConnection = InternetConnect(hOpen, PROXY_NAME, INTERNET_INVALID_PORT_NUMBER, _
            PROXY_USER_NAME, PROXY_USER_PASSWORD, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0)

    如果Ftp Client和Proxy之間是用HTTP溝通

    可否教我可行的程式應是如何??

     

    感激

    2008年6月15日 上午 02:34