Execute, StateChanged, and GetChunk

The Execute method works by sending a command to a remote server located on the Internet. The command might be a request to receive a file, to send a file, or even to delete a directory on the remote computer. The particular server you are talking to defines what commands you can send—the Internet Transfer Control does not define these commands. An FTP server has a complete set of commands that let you do more than transfer files to and from the server—they also let you manage files on the remote computer in a fashion similar to what you might remember from the days of the good old MS-DOS command line.

On the other hand, HTTP servers define a set of commands that let you not only send files to and receive files from the remote server but also transmit header information that describes the various properties of documents stored on the server. This allows summary information about documents to be retrieved without your having to transfer the entire document itself.

Once the server has received the particular command that had been transmitted by the Execute method, it sends a response to your program to let you know what action it has taken. The server’s action is in response to the command it has received. The server can respond to the command by returning some data, performing an internal operation, or even closing the connection. The remote computer informs the calling code of the action it is taking by triggering the StateChanged event of the Internet Transfer Control. The StateChanged event provides a parameter that indicates the particular action the remote computer has just performed. Once this event is triggered, you can take appropriate action in your code depending on what action the remote server is performing.

The most important action you need to take in response to a StateChanged event is to retrieve any data that is waiting. Data is retrieved by using the GetChunk method. The GetChunk method will return, as the name indicates, a “chunk” of data. The size of a chunk is defined when you make the GetChunk call. A chunk is commonly 1024 bytes. This means that if you’ve requested a file that is 30 KB, you’ll have to invoke the GetChunk method roughly 30 times to retrieve all the required data.

The following is the syntax for the Execute command. The URL and operation parameters are required for all types of servers; the data and requestheaders parameters are required only for HTTP transfers.

IcMain.Execute url operation data requestheaders

The process of issuing an Execute method, waiting for the StateChanged event, and then acting on the StateChanged event by using repeated GetChunk method invocations to retrieve data is the standard procedure for using the Internet Transfer Control via the Execute method. Be aware that some actions the server can take result in no data being returned to the calling program (closing a connection or transmitting a file), so the GetChunk method in these cases is not used.

The following section describes using the Execute method with an FTP server. The StateChanged and GetChunk methods are explained in detail after the discussion of the Execute method.