program test;
{$MODE OBJFPC}{$H+}
uses
Classes, SysUtils, StrUtils, fphttpclient;
function GetFileContent(URL: String; Response: TStream): boolean;
begin
Result := False;
With TFPHttpClient.Create(Nil) do
try
Get(URL, Response);
Result := True;
finally
Free;
end;
end;
// W3 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.29) writes:
// The Last-Modified entity-header field indicates the date and time at which
// the origin server believes the variant was last modified.
//
// The exact meaning of this header field depends on the implementation of the
// origin server and the nature of the original resource. For files, it may be
// just the file system last-modified time. For entities with dynamically
// included parts, it may be the most recent of the set of last-modify times
// for its component parts. For database gateways, it may be the last-update
// time stamp of the record. For virtual objects, it may be the last time the
// internal state changed.
function GetModificationDate(URL: String; var Date: String): boolean;
var
HTTPHeaderResponse: TStringList;
Line : string;
begin
Result := False;
Date := 'not available';
HTTPHeaderResponse := TStringList.Create;
HTTPHeaderResponse.NameValueSeparator := ':';
With TFPHttpClient.Create(Nil) do
try
// A Head request does not download the file/URL but only request information
Head(URL, HTTPHeaderResponse);
for line in HTTPHeaderResponse do
begin
if AnsiStartsStr('Last-Modified:', Line) then
begin
Date := ReplaceStr(Line, 'Last-Modified:', '');
Result := True;
Break;
end;
end;
finally
Free;
HTTPHeaderResponse.Free;
end;
end;
procedure HandleURL(URL: String);
var
Date, Line: String;
ResponseData: TMemoryStream;
ResponseAsStrings : TStringList;
begin
// create stream to hold 'downloaded' data
ResponseData := TMemoryStream.Create;
try
WriteLn('-------------' + StringOfChar('-', Length(URL)));
WriteLn('Handling URL ' , URL);
WriteLn('-------------' + StringOfChar('-', Length(URL)));
Date := '';
ResponseData.Position := 0; // Clear; // Size := 0;
if GetModificationDate(URL, Date) then
begin
WriteLn('Last modification date according to server is: ', Date);
if GetFileContent(URL, ResponseData) then
begin
WriteLn('Content of file received');
if AnsiEndsStr('.htm', URL) or AnsiEndsStr('.html', URL) then
begin
Writeln('Showing contents:');
// 'Convert' MemoryStream to StringList
ResponseAsStrings := TStringList.Create;
// Make sure that data can be read from memorystream
ResponseData.Position := 0;
// Load the stringlist from the memorystream
ResponseAsStrings.LoadFromStream(ResponseData);
// Display content of the stringlist
for Line in ResponseAsStrings do WriteLn(Line);
// Clean up
ResponseAsStrings.Free;
end
else
begin
WriteLn('Saving contents to file ', ExtractFilename(URL));
// Save contents of memorystream to a file
ResponseData.SaveToFile(ExtractFilename(URL));
end;
end
else WriteLn('Unable to retrieve content for ', URL);
end
else WriteLn('Unable to retrieve modification date for ', URL);
WriteLn('---- done ----');
finally
// Clean up
ResponseData.Free;
end;
end;
begin
HandleURL('http://forum.lazarus.freepascal.org/Themes/default/images/star.gif');
HandleURL('http://forum.lazarus.freepascal.org/index.php/topic,39123.0.html');
end.