{ KOL MCK } // Do not remove this line!
{$DEFINE KOL_MCK}
unit Unit1;

interface

{$IF Defined(KOL_MCK)}
uses Windows, Messages, KOL, KOLHTTPDownload {$IF Defined(KOL_MCK)}{$ELSE}, mirror, Classes, Controls, mckControls, mckObjs, Graphics,  mckHTTPDownload,
  mckCtrls {$IFEND (place your units here->)};
{$ELSE}
{$I uses.inc}
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;
{$IFEND}

type
  {$IF Defined(KOL_MCK)}
  {$I MCKfakeClasses.inc}
  {$IFDEF KOLCLASSES} {$I TForm1class.inc} {$ELSE OBJECTS} PForm1 = ^TForm1; {$ENDIF CLASSES/OBJECTS}
  {$IFDEF KOLCLASSES}{$I TForm1.inc}{$ELSE} TForm1 = object(TObj) {$ENDIF}
    Form: PControl;
  {$ELSE not_KOL_MCK}
  TForm1 = class(TForm)
  {$IFEND KOL_MCK}
    KOLProject1: TKOLProject;
    KOLForm1: TKOLForm;
    DL: TKOLHttpDownload;
    Label1: TKOLLabel;
    EditBox1: TKOLEditBox;
    Button1: TKOLButton;
    ListBox: TKOLListBox;
    ProgressBar: TKOLProgressBar;
    SaveDlg: TKOLOpenSaveDialog;
    Button2: TKOLButton;
    Label2: TKOLLabel;
    procedure Button1Click(Sender: PObj);
    procedure DLHeaderReceived(Sender: PHTTPDownload;
      HeaderList: PStrList);
    procedure DLProgress(Sender: PHTTPDownload; BytesRecv,
      BytesAll: Integer);
    procedure DLDownload(Sender: PHTTPDownload; Data: PStream);
    procedure DLError(Sender: PHTTPDownload; Error: Word);
    procedure Button2Click(Sender: PObj);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

const
 strDelimiter = '-----------------------------------------------------------------';

var
  Form1 {$IFDEF KOL_MCK} : PForm1 {$ELSE} : TForm1 {$ENDIF} ;

{$IFDEF KOL_MCK}
procedure NewForm1( var Result: PForm1; AParent: PControl );
{$ENDIF}

implementation

{$IFNDEF KOL_MCK} {$R *.DFM} {$ENDIF}

{$IFDEF KOL_MCK}
{$I Unit1_1.inc}
{$ENDIF}

//-----------------------------------------------------
procedure TForm1.Button1Click(Sender: PObj);
var Ext: string;
begin
 //if DL.Busy then Exit;
 ListBox.Clear;
 SaveDlg.Filename:= ExtractFileName( EditBox1.Text );
 Ext:= ExtractFileExt( EditBox1.Text );
 SaveDlg.Filter:= UpperCase(Ext) + ' files|*' + Ext;
 ListBox.Add( 'Retrieving ' + EditBox1.Text );
 ListBox.Add( strDelimiter );
 
 // retrieving specified resource
 DL.GetResource( EditBox1.Text );
end;

//-----------------------------------------------------
procedure TForm1.DLHeaderReceived(Sender: PHTTPDownload;
  HeaderList: PStrList);
var i: Integer;
begin
 for i:= 1 to HeaderList.Count do
   ListBox.Add( HeaderList.Items[i-1] );
 ListBox.Add( strDelimiter );
 Button2.Enabled:= true;
end;

//-----------------------------------------------------
procedure TForm1.DLProgress(Sender: PHTTPDownload; BytesRecv,
  BytesAll: Integer);
begin
 if ( BytesAll > 0 ) then
  ProgressBar.Progress:= Trunc( BytesRecv / BytesAll * 100 );
end;

//-----------------------------------------------------
procedure TForm1.DLDownload(Sender: PHTTPDownload; Data: PStream);
begin
  if SaveDlg.Execute then
  begin
   Data.SaveToFile( SaveDlg.Filename, 0, Data.Size );
   ListBox.Add( 'File saved.');
  end
  else ListBox.Add( 'Saving file cancelled.');
  ListBox.Add( strDelimiter );
end;

//-----------------------------------------------------
procedure TForm1.DLError(Sender: PHTTPDownload; Error: Word);
begin
  MsgOk( 'Error retrieving file: ' + Int2Str( Error ) );
end;

//-----------------------------------------------------
procedure TForm1.Button2Click(Sender: PObj);
var Header: PHTTPHeader;
begin
 New( Header );
 DL.ParseHeaders( Header );
 with Header^, ListBox^ do
 begin
  Add( 'HTTP Version: ' + HTTPVersion);
  Add( 'Status Code: ' + Int2Str( StatusCode ) );
  Add( 'Reason Phrase: ' + ReasonPhrase );
  Add( 'Server Date: ' + ServerDate );
  Add( 'Server: ' + ServerStr );
  Add( 'Last Modified: ' + LastModified );
  Add( 'Location:  ' + Location );
  Add( 'Cookie: ' + SetCookie );
  Add( 'Expires: ' + Expires );
  Add( 'Content Length: ' + Int2Str( ContentLength ) );
  Add( 'Transfer Encoding: ' + TransferEncoding );
  Add( 'Content Type: ' + ContentType );
  Add( '' );
 end;
 Dispose( Header );
end;

//-----------------------------------------------------

end.







