How to download a very simple HTTPS page in Delphi?

Viewed 522

I tried a code I saw here but it didn't work for HTTPS. I need to download this page as a String, and add some Break lines on it to put the informations in order in a TMemo.

How to do it? I tried to use Indy but I failed because of the SSL.

I tried the solutions of this page: How to download a web page into a variable?

How to download this page https://api.rastrearpedidos.com.br/api/rastreio/v1?codigo=OP133496280BR thats just pure text and put in in a String? and also format it like that, in the lines of a TMemo:

"Objeto em trĂ¢nsito - por favor aguarde"
"cidade":"SAO JOSE DOS CAMPOS"
"uf":"SP"
"dataHora":"18/06/2021 16:53"
"descricao":"Objeto postado","cidade":"SAO JOSE DOS CAMPOS","uf":"SP"

It's portuguese, English isn't my first language. Thanks if you guys could help me. I Use the Embarcadero Delphi 10.2 Tokyo.

2 Answers

When using Indy, assuming you are using Indy's default TIdSSLIOHandlerSocketOpenSSL component for SSL/TLS support, then make sure you put the 2 OpenSSL DLLs, ssleay32.dll and libeay32.dll, in the same folder as your EXE. You can get them from here:

https://github.com/IndySockets/OpenSSL-Binaries

Note that TIdSSLIOHandlerSocketOpenSSL only supports up to OpenSSL 1.0.2. If you need to use OpenSSL 1.1.x instead (for TLS 1.3+, etc), then use this SSLIOHandler instead. You will have to obtain the relevant OpenSSL DLLs from elsewhere, or compile them yourself.

Either way, once you decide which SSLIOHandler you want to use, the code is fairly simple:

var
  HTTP: TIdHTTP;
  SSL: TIdSSLIOHandlerSocketOpenSSL;
  Response: string;
begin
  HTTP := TIdHTTP.Create(nil);
  try
    // configure HTTP as needed (version, headers, etc)...

    SSL := TIdSSLIOHandlerSocketOpenSSL.Create(HTTP);
    // configure SSL as needed (TLS versions, certificates, etc)...
    HTTP.IOHandler := SSL;

    Response := HTTP.Get('https://api.rastrearpedidos.com.br/api/rastreio/v1?codigo=OP133496280BR');
  finally
    HTTP.Free;
  end;
  
  // use Response as needed...
end;

You also can use:

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;
Related