TIdHTTP.Head()
が最良の選択肢です。
ただし、別の方法として、最新バージョンではTIdHTTP.Get()
、nil
destination TStream
、またはTIdEventStream
イベント ハンドラを割り当てずに を呼び出しTIdHTTP
、サーバーのデータを読み取りますが、どこにも保存しません。
いずれにせよ、サーバーが失敗の応答コードを送り返した場合TIdHTTP
、例外が発生することにも注意してください (AIgnoreReplies
パラメータを使用して、無視したい特定の応答コード値を指定しない限り)、それも考慮する必要があります。例えば:
procedure Button1Click(Sender: TObject);
var
http : TIdHttp;
url : string;
code : integer;
begin
url := 'http://www.WEBSITE.com';
http := TIdHTTP.Create(nil);
try
try
http.Head(url);
code := http.ResponseCode;
except
on E: EIdHTTPProtocolException do
code := http.ResponseCode; // or: code := E.ErrorCode;
end;
ShowMessage(IntToStr(code));
finally
http.Free;
end;
end;
procedure Button2Click(Sender: TObject);
var
http : TIdHttp;
url : string;
code : integer;
begin
url := 'http://www.WEBSITE.com';
http := TIdHTTP.Create(nil);
try
try
http.Get(url, nil);
code := http.ResponseCode;
except
on E: EIdHTTPProtocolException do
code := http.ResponseCode; // or: code := E.ErrorCode;
end;
ShowMessage(IntToStr(code));
finally
http.Free;
end;
end;
更新:EIdHTTPProtocolException
失敗時に発生するのを避けるためhoNoProtocolErrorException
に、プロパティでフラグを有効にすることができTIdHTTP.HTTPOptions
ます:
procedure Button1Click(Sender: TObject);
var
http : TIdHttp;
url : string;
code : integer;
begin
url := 'http://www.WEBSITE.com';
http := TIdHTTP.Create(nil);
try
http.HTTPOptions := http.HTTPOptions + [hoNoProtocolErrorException];
http.Head(url);
code := http.ResponseCode;
ShowMessage(IntToStr(code));
finally
http.Free;
end;
end;
procedure Button2Click(Sender: TObject);
var
http : TIdHttp;
url : string;
code : integer;
begin
url := 'http://www.WEBSITE.com';
http := TIdHTTP.Create(nil);
try
http.HTTPOptions := http.HTTPOptions + [hoNoProtocolErrorException];
http.Get(url, nil);
code := http.ResponseCode;
ShowMessage(IntToStr(code));
finally
http.Free;
end;
end;