Create an FTP component with VB

zhaozj2021-02-16  77

Basic steps: 1, use an Internetopen to set the setting environment. 2. Call the InternetConnect function to connect to the host. 3, call FTPGETFILE to reach the file. 4. Close the handle created by steps 1, 2, with the InternetCloseHandle function. Now let's take a closer look: 1. Set the environment by calling the Internetopen function. The following is a statement of the specific function of VB calling: Private 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 parameter SAGENT to specify an application or entity that calls the Wininet function. For the purpose of achieving the purpose, FTP control can be set. Parameters LaccessType Specifies that we are directly connected to a host or using the proxy server. If the transfer value 1 is connected directly to the host. If you pass 3, you pass the proxy server. If 0, the registry value proxyNable, ProxyServer, and ProxyOverride is based on HKEY_CURRENT_USER / SOFTWARE / Microsoft / Windows / CurrentVersion / Internet Settings. We can use parameter sproxyname and sproxybypass instead of registration settings to provide proxy servers and IP addresses and names that do not use proxy servers. List the basic format of the agent is Rotocol = protocol: // proxy_name: Access_port? . For example, to specify port 21 on Proxy1 as a proxy server, use ftp = ftp: // proxy1: 21 as a sproxyname. For any host starting with OV?, The sproxybypass string should be OV *? Finally, LFLAGS is used to display different options affecting the results. In our example, we pass 0. So, when you open an Internet session without using a proxy, our call is: lnginet = Internetopen ("ftp control", 1, vbnullstring, vbnullstring, 0) If the function call fails, the LNGINET is 0. Otherwise, the LNGinet saves the value of the handle to be passed to the InternetConnect function in the next step. 2. Connect the Internet to the host by calling the InternetConnect function. VB-specific declaration calling this function is: Private 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

Sservername is the IP address or host name of our FTP server. NserverPort indicates which port connection. The value used in our example is 0, which indicates the default port 21. SUSERNAME and SPASSWORD pass the username and password respectively. LService is used to indicate the type of service used, such as HTTP, FTP, etc. Typically pass the value of 1, indicating the FTP service. If the X8000000 is passed to the LFLAGS parameter, the connection will use the passive FTP semantics. Alternatively, in our example, transfer 0 to use non-passive semantics. Finally, when the callback signal is used, LCONTEXT is used to identify the front-rear relationship of the application. Because the callback signal is not used in our example, this value is 0. Now use anonymous email username and host ftp.microsoft.com: lnginetconn = InternetConnect (LNGINET, "ftp.microsoft.com", 0, _ "anonymous", "ally@wallyworld.com", 1, 0 0) If the function call fails, LNGINETCONN is 0. Conversely, LNGINETCONN is saved in the next step will pass to the value of the handle of FTPGETFILE. 3, now we have implemented connections, then you need to call FTPGETFILE. This function completes all management functions related to reading files on an FTP server and stores locally. VB-specific declaration calling this function is: Private 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 dwflags as long, _ byval dwcontext as long) AS Boolean The first parameter hftpsession is the handle value returned by InternetConnect call. LpszRemoteFile and LPSZNewFile are filenames on the FTP server and file names that will be created on locally. The FFailifeXists flag is 0 (replace local file) or 1 (cancel if the local file already exists). DWFlagsAndAttributes is used to specify file properties for local files. Ignore in our example, only 0 is passed. The dwflags parameter is specified as 1 is transferring files with ASCII (Class A transmission method), specified as 2 is a binary transfer file (1 type transfer method). Since DirMap.txt is an ASCII text file, we pass the value 1. Finally, when using a callback signal, LCONTEXT is used to identify the application before and after the application. Because the callback signal is not used in our example, this value is 0. So, the following is the call to the Dirmap.txt file and exist in C: /DirMap.txt. If the local file already exists, it covers it. BLNRC = ftpgetfile (LNGINETCONN, "DIRMAP.TXT", "C: /Dirmap.txt", 0, 0, 1, 0) If the function call is successful, BLNRC is TRUE, and vice versa False.

4. Now the file has been received, use the InternetCloseHandle to close the connection and session handle. VB Specific Call This function is: Private Declare Function InternetCloseHandle LIB "WinInt.dll" (Byval Hinet As Long) As intended, this function has only one parameter hinet, is the value of the handle to close or abandon . Since there is a handle in InternetConnection and Internetopen, you need to call this closing function twice. In addition, because the InternetConnection handle is determined by the Internetopen handle, the order when it is turned off and the order is in contrast. The following is a call function: InternetCloseHandle LnginetConn InternetCloseHandle LNGINET completes FTP GET with this short four step. Create an FTP component with VB (PUT)

The basic step is: 1, call the Internetopen function setting environment. 2, call the InternetConnect function to connect the host. 3, call the ftpputfile function to get the file. 4. Use the InternetCloseHandle function to close the handle of the first, 2 step. When using ftpgetfile, it looks exactly the same as the above steps. In fact, the only difference is called FTPPUTFILE in step 3. VB-specific declaration calling this function is: Private 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 The first parameter hftpsession is the handle value returned by InternetConnect call. LPSZNewFile and LPSzremotefile are file names on the local machine and file names that will be created on the remote host. When the parameter dwflags is specified as 1, use the ASCII transfer file (Class A transmission method), specified as 2 is a binary transfer file (Class 1 transmission method). Since DirMap.txt is an ASCII text file, we pass the value 1. Finally, when using a callback signal, LCONTEXT is used to identify τ trading. The following is a call to get the Dirmap.txt file and exist in C: /DirMap.txt. BLNRC = ftpputfile (LNGINETCONN, "C :: / Dirmap.txt", "Dirmap.txt", 1, 0) If the function call is successful, BLNRC is TRUE, and vice versa False. You can see that putting the file on the FTP server as simple as the file from the FTP server. One point to pay attention, anonymous users have no right to create files on the FTP server. So to determine that the user account used to connect to the FTP server has the permissions to create a file. Otherwise, the ftpputfile function call returns false, indicating that the PUT failed.

转载请注明原文地址:https://www.9cbs.com/read-16766.html

New Post(0)