0

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

notidaho
  • 588
  • 8
  • 28

2 Answers2

2

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks Remy, sorry lots of newb questions. 1) I assigned TIdHttp1.IOHandler := IdSSLIOHandlerSocket1 but I then get the error "Could not load SSL library". I attached a TIdHttp1.Intercept := IdLogFile which simply reads "Stat Disconnected." 2) Are you suggesting I use GetUrlToFile('/reports.php?report=variable','test.csv'); ? 3) What do you mean by leaking the object? @Remy Lebeau - TeamB – notidaho Oct 26 '11 at 10:10
  • It appears my error is likely due to the wrong SSL dlls. I was using the latest Indy9 library in D5. I loaded the dpk and removed all the components, downloaded the latest 10 and ran the Fulld5.bat. I don't know if this the correct way to upgrade but I get various messages saying IndyProtocols50 does not use or support IdSSLOpenSSLHeaders and IdSSLOpenSSL,IndyX509. – notidaho Oct 26 '11 at 11:24
  • It appears this is a problem with D5. I'll repost as it is a different question: http://stackoverflow.com/questions/7902152/delphi-5-indy-ics-ssl-workaround thanks – notidaho Oct 26 '11 at 11:47
  • 1) SSL oad errors usually mean you are using the wrong OpenSSL DLLs. Indy 9 did not support the official DLLs, it required custom-built DLLs. That was fixed in Indy 10. 2) No, I meant `GetUrlToFile('reports.php?...')`. 3) you have never heard of a memory leak? You are creating a new `TIdHTTP` object but never freeing it. – Remy Lebeau Oct 26 '11 at 22:31
0

Issue 1) has now been resolved in another post: Delphi 5 Indy/ics SSL workaround?

However I would greatly appreciate help on the rest, as follows.

Would I need to make a GET call with the same IdHTTP object and additional URL variable? or should I create a new IdHTTP object?

Would I need to record the session using cookies or can all of this be done with the same call?

Is the GET call above actually what I need to save a csv to file? I may also choose to handle it directly as the data will need importing anyway.

Currently the code gets the error: EIdHTTPProtocolException

Community
  • 1
  • 1
notidaho
  • 588
  • 8
  • 28