Synchronous and Asynchronous Transmission
The OpenURL method results in a
synchronous transmission of data. In this context, synchronous means that
the transfer operation occurs before any other procedures are executed.
Thus the data transfer must be completed before any other code can be
executed.
On the other hand, the Execute method
results in an asynchronous transmission. When the Execute method is
invoked, the transfer operation occurs independently of other procedures.
Thus, after invoking the Execute method, other code can execute while data
is received in the background. www.tartoos.com
What does this mean for the user of the Internet Transfer control? In
short, using the OpenURL method results in a direct stream of data that
you can save to disk (as shown above), or view directly in a TextBox
control (if the data was text). On the other hand, if you use the Execute
method to retrieve data, you must monitor the control's connection state
using the StateChanged event. When the appropriate state is reached,
invoke the GetChunk method to retrieve data from the control's buffer.
This operation is discussed in greater detail below. www.tartoos.com
Using the Execute Method
with the FTP Protocol
The Execute method has four arguments:
url, operation, data, and requestHeaders. FTP operations take only the
operation argument and the url argument, which is optional. For example,
to get a file from a remote computer, you could use the following code:www.tartoos.com
Inet1.Execute "FTP://ftp.microsoft.com", _
"GET
disclaimer.txt C:\Temp\Disclaimer.txt"
If you are used to using FTP to retrieve files from anonymous FTP servers,
you will be familiar with certain commands used to navigate through server
trees, and to retrieve files to a local hard disk. For example, to change
directory with the FTP protocol, you would use the command "CD" with the
path of the directory you wish to change to.
For the most common operations, such as putting a file on a server and
retrieving a file from a server, the Internet Transfer control uses the
same or a similar command with the Execute method. For example, the
following code uses the "CD" command as an argument of the Execute method
to change directory:
' The
txtURL textbox contains the path to open. The
'
txtRemotePath textbox contains the path to change to.
Inet1.Execute txtURL.Text, "CD " & txtRemotePath.Text
Note When using the Execute
method with FTP commands, the data and requestHeaders arguments are not
used. Instead, all of the operations and their parameters are passed as a
single string in the operation argument; parameters are separated by a
space. In the descriptions below, do not confuse the terms "file1" and
"file2" with the data and requestHeaders arguments.
The syntax for FTP operations is:
operationName file1 file2
For example, to get a file, the following code includes the operation name
("GET"), and the two file names required by the operation:
' Get
the file named Disclaimer.txt and copy it to the
'
location C:\Temp\Disclaimer.txt
Inet1.Execute, _
"GET
Disclaimer.txt C:\Temp\Disclaimer.txt"
The following table lists the supported FTP commands of the control: www.tartoos.com
|
Operation
|
Description
|
Example
|
|
CD file1
|
Change Directory. Changes to the
directory specified in file1.
|
Execute , "CD docs\mydocs"
|
|
CDUP
|
Change to Parent. Same as "CD .."
|
Execute , "CDUP"
|
|
DELETE file1
|
Deletes the file specified in
file1.
|
Execute , "DELETE discard.txt"
|
|
DIR [ file1 ]
|
Searches the directory specified in
file1. If file1 isn't supplied, the current working directory is
searched. Use the GetChunk method to return the data.
|
Execute , "DIR /mydocs"
|
|
GET file1 file2
|
Retrieves the remote file specified
in file1, and creates a new local file specified in file2.
|
Execute , _
"GET getme.txt C:\gotme.txt"
|
|
MKDIR file1
|
Creates a directory as specified in
file1. Success is dependent on user privileges on the remote host.
|
Execute , "MKDIR /myDir"
|
|
PUT file1 file2
|
Copies a local file specified in
file1 to the remote host specified in file2.
|
Execute , _
"PUT C:\putme.txt /putme.txt"
|
|
PWD
|
Print Working Directory. Returns the current directory name. Use the
GetChunk method to return the data.
|
Execute , "PWD"
|
|
QUIT
|
Terminate current connection
|
Execute , "QUIT"
|
|
RECV file1 file2
|
Same as GET.
|
Execute , _
"RECV getme.txt C:\gotme.txt"
|
|
RENAME file1 file2
|
Renames a file. Success is dependent on user privileges on the
remote host.
|
Execute ,
"RENAME old.txt new.txt"
|
|
RMDIR file1
|
Remove directory. Success is dependent on user privileges on the
remote host.
|
Execute , "RMDIR oldDir"
|
|
SEND file1
|
Copies a file to the FTP site. (same as PUT.)
|
Execute , _
"SEND C:\putme.txt /putme.txt"
|
|
SIZE file1
|
Returns the size of the file
specified in file1.
|
Execute "SIZE /largefile.txt"
|
Important If your proxy server is a CERN proxy server, direct FTP
connections (using the Execute method) are disallowed. In that case, to
get a file, use the OpenURL method with the Open, Put, and Close
statements, as shown earlier in "Saving to a File Using the OpenURL
Method." You can also use the OpenURL method to get a directory listing by
invoking the method and specifying the target directory as the URL.
Using the Execute Method
with the HTTP Protocol
The HTTP protocol allows client machines to request data from the server
using the GET, HEAD, POST, and PUT commands. These operations are shown in
the following table:
|
Operation
|
Description
|
Example
|
|
GET
|
Retrieves the file named in url.
|
Execute "http://www.microsoft.com" & _
"/default.htm", "GET"
|
|
HEAD
|
Retrieves only the headers of the file named in the URL property.
|
Execute , "HEAD"
|
|
POST
|
Provides additional data to support a request to the remote host.
|
Execute , "POST", strFormData
|
|
PUT
|
Replaces data at the specified URL.
|
Execute , "PUT", "replace.htm"
|
The Common Gateway
Interface and the Execute Method
Many World Wide Web sites offer the ability to search a database. This is
accomplished by using the HTTP protocol's ability to send queries using
the Common Gateway Interface (CGI).
It is not in the scope of this topic to explain the CGI; however, if you
are familiar with the CGI, you can use the Execute method to construct an
application that simulates the behavior of World Wide Web sites. For
example, the code below shows a typical CGI query string:
http://www.findThis2490.com/cgi-bin/find.exe?find=Hangzhou
This same query could be sent using the Execute method as shown below:
Dim
strURL As String, strFormData As String
strURL = "//www.findThis2490.com/cgi-bin/find.exe"
strFormData = "find=Hangzhou"
Inet1.Execute strURL, "POST", strFormData
If you are expecting a result back from a server (as in the example
above), you must use the GetChunk method to retrieve the resulting HTML
document.
Using the State Event
with the GetChunk Method www.tartoos.com
When you are downloading data from a remote computer, an asynchronous
connection will be made. For example, using the Execute method with the
operation "GET", will cause the server to retrieve the requested file.
When the entire file has been retrieved, the State argument will return
icResponseCompleted (12). At that point, you can use the GetChunk method
to retrieve the data from the buffer. This is shown in the example below:
Private Sub Inet1_StateChanged(ByVal State As Integer)
Dim vtData As Variant ' Data variable.
Select Case State
'
... Other cases not shown.
Case icResponseCompleted ' 12
' Open a file to write to.
Open txtOperation For Binary Access _
Write As #intFile
' Get the first chunk. NOTE: specify a Byte
' array (icByteArray) to retrieve a binary file.
vtData = Inet1.GetChunk(1024, icString)
Do While LenB(vtData) > 0
Put #intFile, , vtData
' Get next chunk.
vtData = Inet1.GetChunk(1024, icString)
Loop
Put #intFile, , vtData
Close #intFile
End Select
End
Sub
Logging on to FTP Servers www.tartoos.com
FTP servers come in two flavors: public and private. Public servers, as
suggested by the name, are open to anyone. Private servers, on the other
hand, won't let you log on unless you are a bona fide user of the server.
In either case, the FTP protocol demands that you supply a user name and a
password. The two are used to authenticate a user and allow (or disallow)
subsequent actions.
To log on to public servers the common practice is to log in as
"anonymous," (UserName = "anonymous") and send your e-mail name as the
password. However this process is simplified even further with the
Internet Transfer control. By default, if you do not supply UserName and
Password property values, the control sends "anonymous" as your UserName,
and your e-mail name for the Password.
If you are logging on to a private server, simply set the UserName,
Password, and URL properties as appropriate, and invoke the Execute
method, as shown in the example below:
With
Inet1
.URL = "ftp://ftp.someFTPSite1020.com"
.UserName = "John Smith"
.Password = "mAuI&9$6"
.Execute ,"DIR" ' Returns the directory.
.Execute ,"CLOSE" ' Close the connection.
End
With
After you have invoked the Execute method, the FTP connection will remain
open. You can then continue to use the Execute method to perform other FTP
operations such as CD and GET. When you have completed the session, close
the connection using the Execute method with the CLOSE operation. You can
also close the connection automatically by changing the URL property, and
invoking either the OpenURL or Execute method; such action will close the
current FTP connection, and open the new URL. www.tartoos.com
|