使用 WinInet VB.net 测试与 FTP 服务器的连接

Posted

技术标签:

【中文标题】使用 WinInet VB.net 测试与 FTP 服务器的连接【英文标题】:Test Connection To FTP Server with WinInet VB.net 【发布时间】:2014-12-09 02:28:08 【问题描述】:

我已经搜索过很多网站,其中大部分使用 C++ 或 VB6。

我想测试我的代码是否可以使用 winInet API Vb.net 连接到 FTP 服务器。

首先我知道要声明 wininet.dll。

但我不知道如何知道连接已经连接。

这是我的代码:

Imports System.Net
Imports System.IO
Imports System.Net.Sockets

Public Class Form1
Public Declare Function internetopen Lib "wininet.dll" Alias "InternetOpenA" _
    (ByVal lpszAgent As String, _
     ByVal dwAccessType As Long, _
     ByVal lpszproxyName As String, _
     ByVal lpszproxyBypass As String, _
     ByVal dwflags As Long) As Long

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
Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hFtpSession As Integer, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttributes As Integer, ByVal dwFlags As Integer, ByVal dwContext As Integer) As Boolean
Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hFtpSession As Integer, ByVal lpszLocalFile As String, ByVal lpszRemoteFile As String, ByVal dwFlags As Integer, ByVal dwContext As Integer) As Boolean
Private Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, ByVal lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long
Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, ByVal lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, ByVal lpdwCurrentDirectory As Long) As Long


Private Structure FILETIME
    Dim dwLowDateTime As Long
    Dim dwHighDateTime As Long
End Structure

Private Structure WIN32_FIND_DATA
    Dim dwFileAttributes As Long
    Dim ftCreationTime As FILETIME
    Dim ftLastAccessTime As FILETIME
    Dim ftLastWriteTime As FILETIME
    Dim nFileSizeHigh As Long
    Dim nFileSizeLow As Long
    Dim dwReserved0 As Long
    Dim dwReserved1 As Long
    Dim cFileName As String
    Dim cAlternate As String
End Structure

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

Dim INet As Long
    Dim INetConn As Long
    Dim RC As Boolean
    Dim FileData As WIN32_FIND_DATA
    Dim FileList As String
    Dim MAX_PATH As Long = 0


    Try
        INet = internetopen("FTP_Backup", 1, vbNullString, vbNullString, 0)
        INetConn = InternetConnect(INet, "FTP_SITE", 0, "USER", "PASSWORD", 1, 0, 0)
        RC = FtpGetFile(INetConn, "/public_html/install.log", "D:\", False, 0, 0, 0)

        If RC Then
            MsgBox("Succed")
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub
End Class

【问题讨论】:

我不确定您的 FtpGetFile 是否会失败,因为您需要将文件名作为 3 参数传递。 D:\ 不是在本地创建文件的有效名称。它应该类似于 D:\ + localFileName。 【参考方案1】:

InternetConnect() 如果连接成功则返回会话句柄,否则返回 NULL。所以要测试连接是否已经连接,请在尝试连接后进行检查。

Try
    INet = internetopen("FTP_Backup", 1, vbNullString, vbNullString, 0)
    INetConn = InternetConnect(INet, "FTP_SITE", 0, "USER", "PASSWORD", 1, 0, 0)

    If (INetConn = NULL) then
        'Throw an error, we have failed to connect
    End If

    RC = FtpGetFile(INetConn, "/public_html/install.log", "D:\", False, 0, 0, 0)

    If RC Then
        MsgBox("Succed")
    End If
Catch ex As Exception
    MsgBox(ex.ToString)
Finally
    InternetCloseHandle(INet)
End Try    

另外,你没有清理网络调用,调用 InternetCloseHandle 来清理。

【讨论】:

请提出 1 个问题。有: INet = internetopen("FTP_Backup", 1, vbNullString, vbNullString, 0) 我想知道什么是“FTP_Backup”?我从网站上得到它。所以我真的不知道它是什么 @LeonardFebrianto 如果你用谷歌搜索 API,你会发现很多关于它的信息。 InternetOpen() 中的第一个参数是指定给 HTTP 协议的代理。换句话说,网站/服务器知道它在与什么对话。

以上是关于使用 WinInet VB.net 测试与 FTP 服务器的连接的主要内容,如果未能解决你的问题,请参考以下文章

C++ 使用 WinINet 上传到 FTP 服务器

Wininet & C++:使用 FTP 写入和附加,而不是覆盖,文件

FTP 异步操作上的 ERROR_IO_PENDING [Wininet C++]

WinInet C++ ftp 模板?

使用 wininet.dll api 从 FTP 站点搜索和检索文件名

WinInet:FTP put 中的超时管理