gpt4 book ai didi

德尔福和印地 TIDHTTP : distinguish between "server not found" and "not found" response error

转载 作者:行者123 更新时间:2023-12-02 14:37:02 28 4
gpt4 key购买 nike

我正在使用 indy TIDHTTP 编写一种方法来了解我在互联网上的服务器是否已关闭或同一服务器上的页面地址不可用。

我复制了 stackoverflow 上另一个线程中给出的建议:

try
IdHTTP1.Get(mypage_address);
except
on E: EIdHTTPProtocolException do begin
if e.errorcode=404 then
showmessage('404 File not found');
// use E.ErrorCode, E.Message, and E.ErrorMessage as needed...
end;
end;

但是这样我只能检测服务器响应代码,而不能检测服务器是否根本没有响应。我想这很简单,但我不知道该怎么做?

最佳答案

TIdHTTP 成功向服务器发送请求并将 HTTP 错误回复发送回 TIdHTTP 时,会引发 EIdHTTPProtocolException 异常。如果根本无法访问服务器,则会引发不同的异常(通常为 EIdSocketErrorEIdConnectExceptionEIdConnectTimeout)。

try
IdHTTP1.Head(mypage_address);
except
on E: EIdHTTPProtocolException do begin
ShowMessage(Format('HTTP Error: %d %s', [E.ErrorCode, E.Message]));
end;
on E: EIdConnectTimeout do begin
ShowMessage('Timeout trying to connect');
end;
on E: EIdSocketError do begin
ShowMessage(Format('Socket Error: %d %s', [E.LastError, E.Message]));
end;
on E: Exception do begin
ShowMessage(Format('Error: [%s] %s', [E.ClassName, E.Message]));
end;
end;

关于德尔福和印地 TIDHTTP : distinguish between "server not found" and "not found" response error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23411550/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com