Link to home
Start Free TrialLog in
Avatar of Mathias
MathiasFlag for Germany

asked on

Download File with ICS, IdHTTP or InternetReadfile

Hello,
I have a download function for files. It works in BDS 2006, but now I have CodeGear RAD 2009 und it doesn't :-( I think it's a problem that all strings are now in Unicode. I've tried with ICS und IdHTTP too, but there are also the headers (i couldn't strip them).
Can someone help me to have a workling solution in Delphi 2009?

Thx in advance

Bye, TDS
function TMainForm.GetHTML(AUrl: string): string;
var
  databuffer : array[0..4095] of char;
  ResStr : string;
  hSession, hfile: hInternet;
  dwindex,dwcodelen,dwread,dwNumber: cardinal;
  dwcode : array[1..20] of char;
  res    : pchar;
  Str    : pchar;
begin
   ResStr := '';
   Result := '';
  if pos('http://',lowercase(AUrl))=0 then
    AUrl:='http://'+AUrl;
   MainForm.WriteLog('GetHTML(): ' + AUrl);
  hSession:=InternetOpen('InetURL:/1.0', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
   If assigned(hSession) Then
  try
    hfile:=InternetOpenUrl(hsession, pchar(AUrl), nil, 0, INTERNET_FLAG_RELOAD, 0);
      if assigned(hfile) then
      try
      dwIndex  := 0;
      dwCodeLen := 10;
      HttpQueryInfo(hfile, HTTP_QUERY_STATUS_CODE, @dwcode, dwcodeLen, dwIndex);
      res := pchar(@dwcode);
      dwNumber := sizeof(databuffer)-1;
      if (res ='200') or (res ='302') then
      begin
        while (InternetReadfile(hfile,@databuffer,dwNumber,DwRead)) AND (dwRead <>0) do
        begin
          databuffer[dwread]:=#0;
          Str := pchar(@databuffer);
          resStr := resStr + Str;
          Application.ProcessMessages;
        end;
      end
      else
        ResStr := 'Status: '+res;
      finally
        InternetCloseHandle(hfile);
     end;
  finally
    InternetCloseHandle(hsession);
  end;
   MainForm.WriteLog('GetHTML(): ' + ResStr);
   Result := ResStr;
   Result := '';
end;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of smot
smot

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial