This is a sample that shows the use of the HTTP Client API, specifically how to implement a multi-threaded download.
You can find the HttpDownload sample project at:
- Start | Programs | Embarcadero RAD Studio Alexandria | Samples and then navigate to either:
- Subversion Repository for Delphi: https://github.com/Embarcadero/RADStudio10.4Demos/tree/master/Object%20Pascal/RTL/HttpDownload
- Subversion Repository for C++: https://github.com/Embarcadero/RADStudio10.4Demos/tree/master/CPP/RTL/HttpDownload/
This application uses the HTTP Client API to download a file. It separates a file into 4 parts and uses multiple threads to download the parts simultaneously.
The application uses the following controls:
EditFileName: Allows you to specify the name of the file.
EditURL: Allows you to provide a URL of a file.
BStartDownload: Fires the
LabelGlobalSpeed: Displays the average download speed.
ImageList1: Holds the "stop" button images.
Button4: Fire the
ProgressBarPart4: Display the progress of the download of the corresponding file part.
Label4: Display the download speed of the corresponding file part.
Memo1: Displays the messages from the application for the user.
How to Use the Sample
- Navigate to one of the locations given above and open:
- Delphi: HttpDownloadDemo.dproj.
- C++: HttpDownloadDemo.cbproj.
- Press F9 or choose Run > Run.
- Enter the file name for the file that you want to download.
- Enter the URL of the file that you want to download.
- Click the Start download button control.
- Note: The implementation of the multi-threaded download requires the resume-download feature. If the file that you wish to download is on a server that does not support the resume-download feature, the application will display a message "Server has NOT resumed download feature".
The location where the file is downloaded depends on the operating system. On Windows, the location is the
MyDocuments folder. See GetDocumentsPath for more information.
Stop / Restart the Download
To stop the download of a part of the file, press the "stop" button (the red square) next to the corresponding progress bar. When the download is complete or if you stop the download of all 4 parts of the file, you can restart the download of the file by pressing the
Start Download button.
This application uses an instance of THTTPClient to:
- connect to the server
- check if the server supports the resume-download feature
- start the download of the file.
This application also implements a custom class TDownloadThread to handle the simultaneous download of parts of the file.
On initialization, the
FormCreate groups the button controls, the progress bar controls and the labels into corresponding arrays.
- The application defines the following event handlers:
ButtonCancelClick: Disables the button control that fires this handler.
BStartDownloadClick: Disables the
BStartDownloadbutton control and calls
SampleDownload. After the download is completed (or aborted),
ReceiveThreadDataEvent: Occurs when an instance of TDownloadThread receives data from its HTTP client. This handler checks whether the download is aborted. If not, it updates the progress bar control and the speed label control. It also calls the ProcessMessages method, which allows the application to refresh other controls while the download threads run. TDownloadThread.ReceiveDataEvent is the dispatcher of this event.
- The application also defines the following methods:
SampleDownload: Creates an instance of HTTP client and checks whether the server that hosts the file at the provided URL supports resume-download feature. If yes, the method does the following:
- Reserves the space for the file to be downloaded.
- Splits the file into four parts and assigns each part to a new instance of TDownloadThread.
- Starts the download (calls the Start method of each thread).
- Waits until the download is completed and updates the
LabelGlobalSpeedcontrol with the calculated average speed of the download.
- Otherwise, the application aborts the download process and displays the following text in the
Memo1control: "Server has NOT resumed download feature".
TDownloadThread is a descendant of TThread. This class handles the multi-threading functionality of the application.
- TDownloadThread defines the following methods:
Createis the constructor.
Destroyis the destructor.
Executeis the implementation of the Execute method that runs when a thread is run.
Executecreates its own instance of THTTPClient and assigns the OnReceiveData event of that client to the
ReceiveDataEventis the dispatcher on the custom TDownloadThreadDataEvent event. This dispatcher also calculates the current speed of the download.