作者: Jim Wang 公众号: 巴博萨船长

InnoSetup 检查网络连接

1. 循环检查网络连接直至连接成共,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function IsNetWorkActivatedRepeated: boolean;
var
WinHttpReq: Variant;
Connected: Boolean;
begin
Connected := False;
repeat
Log('Checking connection to the server');
try
WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
{ Use your real server host name }
WinHttpReq.Open('GET', 'https://www.camtek.de/', False);
WinHttpReq.Send('');
Log('Connected to the server; status: ' + IntToStr(WinHttpReq.Status) + ' ' +
WinHttpReq.StatusText);
Connected := True;
except
Log('Error connecting to the server: ' + GetExceptionMessage);
if WizardSilent then
begin
Log('Connection to the server is not available, aborting silent installation');
Result := False;
Exit;
end
else
if MsgBox('Cannot reach server. Please check your Internet connection.',
mbError, MB_RETRYCANCEL) = IDRETRY then
begin
Log('Retrying');
end
else
begin
Log('Aborting');
Result := False;
Exit;
end;
end;
until Connected;

Result := True;
end;

2. 有限次数检测网络,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function IsNetWorkActivatedTried: boolean;
var
WinHttpReq: Variant;
Connected: Boolean;
iTriedTime: Integer;
begin
Connected := False;
iTriedTime := 0;
repeat
iTriedTime := iTriedTime + 1;
Log('Checking connection to the server, try ' + + IntToStr(iTriedTime) + ' time.');
try

WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
WinHttpReq.SetTimeouts('3000', '3000', '3000', '3000');
// Use your real server host name
WinHttpReq.Open('GET', 'https://www.goolge.de/', False);
WinHttpReq.Send('');
Log('Connected to the server; status: ' + IntToStr(WinHttpReq.Status) + ' ' +
WinHttpReq.StatusText);
Connected := True;
except
Log('Error connecting to the server, msg: ' + GetExceptionMessage + 'try again! ');
end;
until (iTriedTime = 3) or (Connected = True) ;
Result := Connected;
end;

版权声明:
文章首发于 Jim Wang's blog , 转载文章请务必以超链接形式标明文章出处,作者信息及本版权声明。