我已经使用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
答案 0 :(得分:0)
如果连接成功,则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。