这里有些东西不工作,我想知道是否有人可以看到下面代码中的错误。
function CheckUrl(url: String): Boolean;
var
sResp: String;
begin
Result := False;
try
sResp := IdHTTP1.Get(url);
except
on E: EIdHTTPProtocolException do Result := False;
on E: EIdConnClosedGracefully do Result := False;
on E: EIdSocketError do Result := False;
on E: EIdException do Result := False;
on E: Exception do Result := False;
end;
if IdHTTP1.ResponseCode = 200 then Result := True;
end;
我在主窗体的OnShow事件中使用这个函数:
procedure TForm1.FormShow(Sender: TObject);
var
urlOk: boolean;
begin
//code1
if not CheckURL(Url) then
begin
//code2
end;
//some code here
end;
网络连接不可用时出现问题。即使我在CheckUrl函数中使用try-except方法,并处理所有异常,如果发生异常且code2未执行,CheckUrl函数也不会返回False。也许有人能看出我的错误,给我指出正确的方向。谢谢。
你可以大大简化你的函数如下:
function CheckUrl(url: String): Boolean;
begin
try
// using AResponseContent=nil to discard any data received so as not to waste any memory storing it temporarily...
IdHTTP1.Get(url, TStream(nil));
Result := True;
except
Result := False;
end;
end;
另外:
function CheckUrl(url: String): Boolean;
begin
try
IdHTTP1.Head(url);
Result := True;
except
Result := False;
end;
end;
如果仍然没有返回预期的结果,那么您的项目或IDE安装存在严重问题。