Download CSV in Delphi 5 with Indy

青春壹個敷衍的年華 提交于 2019-12-13 04:51:14

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/7895790/download-csv-in-delphi-5-with-indy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!