delphi 检测一个网络连接是否有效带超时


本文整理自网络,侵删。

 //检测一个网络连接是否有效
function CheckUrl(url: string; TimeOut:integer=20000): boolean;
var
hSession, hfile, hRequest: hInternet;
dwindex, dwcodelen: dword;
dwcode: array[1..20] of char;
res: pchar;
re: integer;
Err1: integer;
j: integer;
begin
if pos('http://', lowercase(url)) = 0 then
url := 'http://' + url;
Result := false;
hSession := InternetOpen('Mozilla/4.0', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
InternetSetOption(hSession, Internet_OPTION_CONNECT_TIMEOUT, @TimeOut, 4);
if assigned(hsession) then
begin
j := 1;
while true do
begin
hfile := InternetOpenUrl(hsession, pchar(url), nil, 0, INTERNET_FLAG_RELOAD, 0);
if hfile = nil then
begin
j := j + 1;
Err1 := GetLastError;
if j > 5 then break;
if (Err1 <> 12002) or (Err1 <> 12152) then break;
sleep(2000);
end
else begin
break;
end;
end;
dwIndex := 0;
dwCodeLen := 10;
HttpQueryInfo(hfile, HTTP_QUERY_STATUS_CODE, @dwcode, dwcodeLen, dwIndex);
res := pchar(@dwcode);
re := strtointdef(res, 404);
case re of
200..299: result := True;
401..403: result := True;
else result := False;
end;
if assigned(hfile) then
InternetCloseHandle(hfile);
InternetCloseHandle(hsession);
end;
end;

相关阅读 >>

Delphi sendemail 邮件发送单元

Delphi 如何使用程序标识符检查程序是否已安装

Delphi tdragimage v0.1 Delphi 图片拖拽组件 带有加速度 碰撞效果

Delphi 数据库处理密码防止sql注入

Delphi tstringlist.find

Delphi ile dataset to xml

Delphi 安卓读写ini文件

Delphi indy防止假死

Delphi分享pos函数。(比fastpos还要快)

Delphi 取得ie下面输入框内容

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...