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

Delphi

Avatar of undefined
Last Comment
smot

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
smot

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck