Aufgrund einiger Problemchen hier nocheine Variante:
Delphi-Quellcode:
uses
IdHeaderList, IdHTTP;
type
TCheckHeadersAvailable = class
class procedure Check(Sender: TObject;
AHeaders: TIdHeaderList; var VContinue: Boolean);
end;
class procedure TCheckHeadersAvailable.Check(Sender: TObject;
AHeaders: TIdHeaderList; var VContinue: Boolean);
begin
VContinue := False;
end;
function UrlCheck(sUrl: String; bAllowRedirects: Boolean = false): Integer;
begin
with TIdHTTP.Create(nil) do
try
HandleRedirects := bAllowRedirects;
OnHeadersAvailable := TCheckHeadersAvailable.Check;
try
Head(sUrl);
Result := Response.ResponseCode;
except
if (Response.ResponseCode >= 400) and (Response.ResponseCode < 600) then
Result := Response.ResponseCode else Result := 503;
end;
finally
Free;
end;
end;
Delphi-Quellcode:
procedure TForm5.Button1Click(Sender: TObject);
var
C: Integer;
begin
C := UrlCheck('http://www.google.de/mdklsandksj.txt', True);
if C = 200 then ShowMessage('OK')
else ShowMessage(Format('Fehler %d', [C]));
end;
oder
Delphi-Quellcode:
procedure TForm5.Button1Click(Sender: TObject);
begin
if UrlCheck('http://www.google.de/mdklsandksj.txt', True) = 200 then
ShowMessage('OK')
else
ShowMessage('Fehler');
end;
Ach ja, GetLastError (siehe Post 13) als Result ist keine gute Idee.