1. Home
  2. Computing & Technology
  3. Delphi Programming

Programmatically Download Web Documents from the Internet
Download Files, HTML pages, RSS feeds, ...

By Zarko Gajic, About.com

If your program relies on Packages or DLLs than deploying new versions of your libraries certainly takes some time. The Internet provides a fairly easy way to accomplish this task. Adding an auto-update option to your applications could be the best way to keep your Delphi applications up to date.

Let's see how to create the most important piece of code in any FTP application.

Delphi gives us full access to the WinInet API (wininet.pas) which we can use to connect to and retrieve files from any Web site that uses either Hypertext Transfer Protocol (HTTP) or File Transfer Protocol (FTP). For example, we could use the functions inside the WinInet API to: add an FTP browser to any application, create an application that automatically downloads files from a public FTP site or search the Internet site for references to graphics and download only the graphics.

Download Internet File

uses WinInet;

function GetInetFile (const fileURL, FileName: String): boolean;
const
  BufferSize = 1024;
var
  hSession, hURL: HInternet;
  Buffer: array[1..BufferSize] of Byte;
  BufferLen: DWORD;
  f: File;
  sAppName: string;
begin
 result := false;
 sAppName := ExtractFileName(Application.ExeName) ;
 hSession := InternetOpen(PChar(sAppName), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0) ;
 try
  hURL := InternetOpenURL(hSession, PChar(fileURL), nil, 0, 0, 0) ;
  try
   AssignFile(f, FileName) ;
   Rewrite(f,1) ;
   repeat
    InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen) ;
    BlockWrite(f, Buffer, BufferLen)
   until BufferLen = 0;
   CloseFile(f) ;
   result := True;
  finally
   InternetCloseHandle(hURL)
  end
 finally
  InternetCloseHandle(hSession)
 end
end;

Note: In order to provide some visual feedback to the user you could add a line like FlashWindow(Application.Handle,True) in the body of the repeat/until block. The FlashWindow API call flashes the caption of your applications name in the task bar.

Or, animate the caption of the button on the TaskBar.

To call the GetInetFile function you could use the next piece of code:

var
  internetFile,
  localFileName: string;
begin
 internetFile := 'http://z.about.com/6/g/delphi/b/index.xml';
 localFileName := 'About Delphi Programming RSS Feed.xml';

 if GetInetFile(internetFile, localFileName) then
   ShowMessage('Download successful.')
 else
   ShowMessage('Error in file download.') ;
This code will get the RSS feed from this site, download it, and save it as 'About Delphi Programming RSS Feed.xml'.

Use the RSS Feed to create a simple Tree-Like RSS Reader.

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Internet / Network
  5. Programmatically Download Web Documents from the Internet using Delphi

©2009 About.com, a part of The New York Times Company.

All rights reserved.