2010-10-02 60 views
2

我試圖找到一個簡單的方法來通過FTP檢查用戶狀態,wininet似乎是最好的選擇。 如何從命令獲得輸出?使用wininet.dll檢索FTP命令輸出

Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal HINet As Integer) As Integer 
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Integer, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Integer) As Integer 
Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Integer, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Integer, ByVal lFlags As Integer, ByVal lContext As Integer) As Integer 
Public Declare Function ftpCommand Lib "wininet.dll" Alias "FtpCommandA" (ByVal hConnect As Integer, ByVal fExpectResponse As Boolean, ByVal dwFlags As Integer, ByVal lpszCommand As String, ByRef dwContext As Integer, ByRef phFtpCommand As Integer) As Boolean 

Dim INet, INetConn As Integer 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 

    INet = InternetOpen("MyFTP", 1, vbNullString, vbNullString, 0) 
    INetConn = InternetConnect(INet, "192.168.1.6", 21, "user", "pwd", 1, 0, 0) 

    strCommand = "SITE SHOW SERVER 192.168.1.6.21" 

    Dim retv As Long 
    Dim Test = ftpCommand(INetConn, True, 2, strCommand, 0, retv) 
    Debug.Write(Test) 

    InternetCloseHandle(INetConn) 
    InternetCloseHandle(INet) 

End Sub 

我期待的輸出是:

Response: 200- Server IP = "192.168.1.6" 
Response: 200- Port = "21" 
Response: 200- Start time = "10/02/2010 02:46:57 PM" 
Response: 200- Download = "0.000 KB" 
Response: 200- Upload = "0.000 KB" 
Response: 200- Online Users = "0" 
Response: 200-====================================== 
Response: 200 Site command OK 

感謝。

回答

1

我想你可能會發現使用InternetGetLastResponseInfo這是C++的定義

(由導航WinINet function list發現這一點)的鏈接,但提供了一個有用的提示

它說:

InternetGetLastResponseInfo函數

檢索最新的錯誤描述或服務器響應對調用此函數的線程。

考慮這個樣本實施

Private Function GetServerResponse() As String 
    Dim lError As Long 
    Dim strBuffer As String 
    Dim lBufferSize As Long 
    Dim retVal As Long 
    retVal = InternetGetLastResponseInfo(lError, strBuffer, lBufferSize) 
    strBuffer = New String("", lBufferSize + 1) 
    retVal = InternetGetLastResponseInfo(lError, strBuffer, lBufferSize) 
    GetServerResponse = strBuffer 
End Function 

它調用外部函數(這裏複製粘貼接入)

Declare Function InternetGetLastResponseInfo Lib "wininet" Alias "InternetGetLastResponseInfoA" (ByRef lpdwError As Long, ByVal lpszBuffer As String, ByRef lpdwBufferLength As Long) As Boolean 

兩次,第一次得到的緩衝區長度和重新填充緩衝區字符串然後返回填充的緩衝區