Search code examples
delphipostindydelphi-5

Download CSV in Delphi 5 with Indy


I know there's alot of Indy threads but I can't get one to match my case.

I have been given a URL with a username and password form. this then actions to a URL/reports.php on which there are multiple hyperlinks.

Each of these links will direct to a page with URL variables e.g. reports.php?report=variablename where a download will immediately start.

My thinking so far:

procedure TForm1.PostData(Sender: TObject);
var
  paramList:TStringList;
  url,text:string;
//  IdHTTP1: TIdHTTP;
  IdSSLIOHandlerSocket1: TIdSSLIOHandlerSocket;
  idLogFile1 : TidLogFile;
begin

  idLogFile1 := TidLogFile.Create(nil);
  with idLogFile1 do
  begin
  idLogFile1.Filename := 'C:\HTTPSlogfile.txt';
  idLogFile1.active := True;
  end;

  IdHTTP1 := TIdHTTP.Create(nil);
  IdSSLIOHandlerSocket1 := TIdSSLIOHandlerSocket.Create(nil);
  IdSSLIOHandlerSocket1.SSLOptions.Method := sslvSSLv23;
  IdHTTP1.IOHandler := IdSSLIOHandlerSocket1;


  IdHTTP1.HandleRedirects := true;
  IdHTTP1.ReadTimeout := 5000;
  IdHTTP1.Intercept := idLogFile1;

  paramList:=TStringList.create;
  paramList.Clear;
  paramList.Add('loguser=testuser');
  paramList.Add('logpass=duke7aunt');
  paramList.Add('logclub=8005');
  url := 'https://www.dfcdata.co.uk/integration/reports.php?report=live';

   try
   IdHTTP1.Post(url,paramList);
 except
  on E:Exception do
   begin
    showMessage('failed to post to: '+url);
    ShowMessage('Exception message = '+E.Message);
   end;
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
 reportType : String;
begin
  PostData(Self);
  reportType := 'live';
  GetUrlToFile('',reportType+'.csv');
end;


procedure TForm1.GetUrlToFile(AURL, AFile : String);
var
 Output : TMemoryStream;
 success : Boolean;
begin
  success := True;
  Output := TMemoryStream.Create;
  try
    try
     IdHTTP1.Get(AURL, Output);
     IdHTTP1.Disconnect;
     except
     on E : Exception do
     begin
       ShowMessage('Get failed to GET from '+IdHTTP1.GetNamePath +'. Exception message = '+E.Message);
       success := False;
    end;
    end;

    if success = True then
    begin
    showMessage('Filed saved');
    Output.SaveToFile(AFile);
    end;
  finally
    Output.Free;
  end;
end;

On each try I get "IOHandler is not valid" error. Obviously I'm not posting correctly to the initial page but can anyone advise me on what I'm missing? Also can I simply then hit the download URL after login or will I have to use cookies?

Thanks


Solution

  • There are several bugs in your code:

    1) PostData() is requesting an HTTPS URL, but it is not assigning an SSL-enabled IOHandler to the TIdHTTP.IOHandler property. You need to do so.

    2) Button1Click() is passing a URL to GetUrlToFile() that does not specify any protocol, so TIdHTTP will end up treating that URL as relative to its existing URL, and thus try to download from https://www.testurl.com/test/testurl.com/test/reports.phpinstead of https://testurl.com/test/reports.php. If you want to request a relative URL, don't include the hostname (or even the path in this case, since you are sending multiple requests to the same path, just different documents).

    3) you are leaking the TIdHTTP object.