Friday, November 1, 2013

Fetching files using https (SSL) using Delphi for Android

Copy & paste solution, which logs in, supports https and returns the result as one large string:

uses
  Classes, SysUtils,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP,
  IdSSLOpenSSL;

function HttpsAuthGet(Url, Username, Password: string):string;
var
  ssl: TIdSSLIOHandlerSocketOpenSSL;
  http: TIdHTTP;
begin
  http := TIdHttp.Create(nil);
  try
    ssl := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
    try
      http.IOHandler := ssl;
      http.Request.BasicAuthentication:=True;
      http.Request.Username := Username;
      http.Request.Password := Password;
      Result:=http.Get(Url);
      if (http.ResponseCode<200) or (http.ResponseCode>=300) then
        raise Exception.Create(http.ResponseText);
    finally
      FreeAndNil(ssl);
    end;
  finally
    FreeAndNil(http);
  end;
end;

No comments:

Post a Comment