RTL.HttpAsyncDownload Sample

From RAD Studio Code Examples
Jump to: navigation, search

This is an example of how to use the HTTP Client API to download a file using an asynchronous HTTP request.

Location

You can find the HttpDownload sample project at:

  • Start | Programs | Embarcadero RAD Studio Athens | Samples and then navigate to either:
    • Object Pascal\RTL\HttpAsyncDownload
    • CPP\RTL\HttpAsyncDownloadDemo
  • 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.

Description

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 BStartDownloadClick event handler.
  • LabelGlobalSpeed: Displays the average download speed.
  • ImageList1: Holds the "stop" button image.
  • BStopDownload: Fires the ButtonCancelClick event handler.
  • ProgressBarDownload: Displays the progress of the download.
  • Memo1: Displays the messages from the application for the user.

How to Use the Sample

  1. Navigate to one of the locations given above and open:
    • Delphi: HttpAsyncDownloadDemo.dproj.
    • C++: HttpAsyncDownloadDemoCPP.cbproj.
  2. Press F9 or choose Run > Run.
  3. Enter the file name for the file that you want to download.
  4. Enter the URL of the file that you want to download.
  5. 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.

Implementation

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.

TFormDownloadDemo

On initialization, the FormCreate:

The application defines the following event handlers:

  • ButtonCancelClick: Disables the button control that fires this handler and calls IAsyncResult.Cancel to stop asynchronous transfer.
  • BStartDownloadClick: Disables the BStartDownload button control and calls SampleDownload. After the download is completed (or aborted), BStartDownloadClick enables the BStartDownload button control.

The application also defines the following methods:

  • SampleDownload:
    • 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 ProgressBarDownload control.
  • ReceiveDataEvent:
    • 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 ProgressBarDownload control accordingly.
    • Updates the text from the LabelGlobalSpeed control with the current speed.

Uses

See Also