We help IT Professionals succeed at work.
Get Started

Download File with ICS, IdHTTP or InternetReadfile

Mathias
Mathias asked
on
1,152 Views
Last Modified: 2013-11-23
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

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE