Delphi HTTP/HTTPS Client Component

HTTP is an industry-standard protocol that underlies any data exchange over the Internet between a client, usually a web browser, and a server that hosts resources, such as HTML documents. HTTPS is the secure version of HTTP that uses TLS/SSL encryption for the transferred data.

SecureBridge offers Delphi components for implementing an HTTP/HTTPS client: the TScHTTPWebRequest component and the TScHttpWebResponse class. You can also develop REST applications in Delphi using the TScHttWebRequest component. Our Delphi HTTP component implements both HTTP 1.0 and HTTP 1.1 specifications and provides a simple way to develop client applications that communicate with a web server over TLS/SSL secured connections (TLS 1.0-1.3 and SSL 3.0 protocols).

Creating an HTTP/HTTPS Client in Delphi (RAD Studio 10.3)

To create a simple HTTP client that sends an HTTP GET request in Delphi, create a Windows VCL application and drop the TScHttpWebRequest, TMemo, and TButton components onto the form. Switch to the code editor and add the System.JSON module to the uses section of your application. Assuming that default names are used for the instances of components in your project, create an OnClick event handler for the button on the form and add the following code:

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  Request: TScHttpWebRequest;
  Response: TScHttpWebResponse;
  ResponseStr: String;
  JSonValue: TJSonValue;
  JsonArray: TJsonArray;
  Name: String;
  UserName: String;
  Email: String;
begin
  Request := TScHttpWebRequest.Create('https://jsonplaceholder.typicode.com/users');
  try
    Response := Request.GetResponse;	
    try
      if Request.IsSecure then begin
      ResponseStr := Response.ReadAsString;
      JsonArray := TJSonObject.ParseJSONValue(ResponseStr) as TJsonArray;
      try
        for i:=0 to JsonArray.Count - 1 do begin
          JsonValue := JsonArray.Items[i];
          Name := JsonValue.GetValue('name');
          UserName := JsonValue.GetValue('username');
          Email := JsonValue.GetValue('email');
          Memo1.Lines.Add('Name: ' + Name + ' - UserName: ' + UserName + ' - Email: ' + Email);
        end;
      finally
        JsonArray.Free;
      end;
      end;
    finally
      Response.Free;
    end;
  finally
    Request.Free;
  end;
end;
						

In the above code, we use the Create constructor to create an instance of the TScHttpWebRequest class and initialize it with the specified URI scheme, i.e. we specify the resource that we want to fetch over HTTP. Then we call the GetResponse method of the TScHttpWebRequest instance, which creates a connection to the web server, sends a request, and creates and returns a TScHttpWebResponse instance that holds the response from the web resource.

The IsSecure property determines whether a connection between the client and the server is encrypted by TLS/SSL protocol. The ReadAsString method is called on the TScHttpWebResponse instance to read the body of the response from the web server and return a string containing the body of the response.

Then we parse JSON values from the received string and store them as an array in the instance of the TJsonArray class. Finally, we iterate over the array to extract the needed values and add them to the TMemo instance. To test the Delphi HTTPS client application, press F9 to compile the app and click on the button.

Delphi HTTP Client Component

Here’s an example that uses the HTTP POST method in the TScHttpWebRequest component to send data to the web server from Delphi application:

procedure TForm1.Button1Click(Sender: TObject);
var
  Request: TScHttpWebRequest;
  Response: TScHttpWebResponse;
  DataStream: TStream;
  ResponseStr: string;
begin
  Request := TScHttpWebRequest.Create(URL);
  try
    DataStream := TFileStream.Create(Filename, fmOpenRead);
    try
      Request.Method := rmPost;
      Request.ContentType := 'application/x-www-form-urlencoded';
      Request.ContentLength := DataStream.Size;
      Request.SendChunked := True;
      Request.RequestStream := DataStream;
      Response := Request.GetResponse;
      ResponseStr := Response.ReadAsString;
      Memo1.Lines.Add(ResponseStr);
      Response.Free;
    finally
      DataStream.Free;
    end;
  finally
    Request.Free;
  end;
end;
						

As you can see from these examples, creating an HTTP/HTTPS client in Delphi using SecureBridge components does not take much effort and speeds up your development process. You can download the fully-featured trial version to evaluate the solution for 60 days.

You may also find interesting:
- Delphi SSL Client
- Delphi WebSocket Client