This is an example of how to use the HTTP Client API to download a file using an asynchronous HTTP request.
You can find the HttpDownload sample project at:
- Start | Programs | Embarcadero RAD Studio Rio | Samples and then navigate to either:
- Subversion Repository:
- You can find Delphi and C++ code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
This application uses the HTTP Client API to download a file. The download is an asynchronous call that does not block the application.
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 image.
BStopDownload: Fires the
ProgressBarDownload: Displays the progress of the download.
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: HttpAsyncDownloadDemo.dproj.
- C++: HttpAsyncDownloadDemoCPP.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.
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 the file, press the "stop" button (the red square) next to the progress bar. When the download is complete or if you stop the download of the file, you can restart the download of the file by pressing the
Start Download button.
This application creates an instance of THTTPClient to:
- Connect to the server.
- Receive response data for one or more requests from the server.
- Start the download of the file with an asynchronous HTTP request.
On initialization, the
The application defines the following event handlers:
ButtonCancelClick: Disables the button control that fires this handler and calls
IAsyncResult.Cancelto stop asynchronous transfer.
BStartDownloadClick: Disables the
BStartDownloadbutton control and calls
SampleDownload. After the download is completed (or aborted),
The application also defines the following methods:
- Sends an HTTP request to the specified URL using the HEAD HTTP request method.
- Adds the response from the server to the TMemo: the StatusCode and the StatusText.
- Creates the file that is going to be dowloaded.
- Starts the download process by calling BeginGet which starts an asynchronous HTTP request using the GET HTTP request method.
- Specifies Min and Max values for the
- This method is called when the OnReceiveData from the THTTPClient fires.
- The OnReceiveData event handler fires while your HTTP client receives response data for one or more requests, and it indicates the current progress of the response download for the specified request.
- Calculates the speed of the download and updates the current position of the
- Updates the text from the
LabelGlobalSpeedcontrol with the current speed.