00 Votes

Delphi/Lazarus: Automatically sign in to an HTTPS page or website

Question by Guest | 2018-08-22 at 23:29

How can I log in to an HTTPS or HTTP website automatically via a Lazarus or Delphi program and then automatically read the content of that page?

What do I need to do this?

Who can help me with some code?

Thank you in advance!

ReplyPositiveNegative
4Best Answer4 Votes

Basically, you can proceed in a similar way as when reading the HTML of a website. Since you want to support HTTPS, you need the appropriate SSL libraries. These are ssleay32.dll and libeay32.dll (for a Win 32 program), which should be in the folder of the application.

To simulate the login, you have to imitate the submit process of the login form on the login page. This is usually done through a POST request, which might look something like this:

uses fphttpclient;
 
procedure DoLogin;
var
  AHTTPClient: TFPHTTPClient;
  APostValues: TStringList;
  AUrl, AResult: string;
begin
  AUrl := 'https://www.example.com/login.php';

  AHTTPClient := TFPHTTPClient.Create(nil);
  try
    AHTTPClient.AllowRedirect := True;
    APostValues := TStringList.Create;
    try
      APostValues.Add('name=MyName');
      APostValues.Add('password=MyPassword');
      try
        AResult := AHTTPClient.SimpleFormPost(AUrl, APostValues);
        ShowMessage(AResult);
      except
        on E: exception do ShowMessage(E.Message);
      end;
    finally
      APostValues.Free;
    end;
  finally
    AHTTPClient.Free;
  end;
end; 

As values we use name = MyName and password = MyPassword. Here you have to replace "name" and "password" with the correct names of the input fields used in the HTML of the login page. Of course, you also have to set the correct login data instead of MyName and MyPassword to make your login successful.
2018-08-23 at 22:25

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.