Delphi 减少InternetOpenUrl超时?
此函数检查是否可以访问URL:Delphi 减少InternetOpenUrl超时?,delphi,wininet,delphi-xe7,Delphi,Wininet,Delphi Xe7,此函数检查是否可以访问URL: uses wininet, System.Types, ... function CheckUrl(url: string): boolean; var hSession, hfile, hRequest: hInternet; dwindex, dwcodelen: dword; dwcode: array [1 .. 20] of char; res: pchar; begin if pos('http://', lowercase(u
uses wininet, System.Types,
...
function CheckUrl(url: string): boolean;
var
hSession, hfile, hRequest: hInternet;
dwindex, dwcodelen: dword;
dwcode: array [1 .. 20] of char;
res: pchar;
begin
if pos('http://', lowercase(url)) = 0 then
url := 'http://' + url;
Result := false;
hSession := InternetOpen('InetURL:/1.0', INTERNET_OPEN_TYPE_PRECONFIG,
nil, nil, 0);
if assigned(hSession) then
begin
hfile := InternetOpenUrl(hSession, pchar(url), nil, 0,
INTERNET_FLAG_RELOAD, 0);
dwindex := 0;
dwcodelen := 10;
HttpQueryInfo(hfile, HTTP_QUERY_STATUS_CODE, @dwcode, dwcodelen, dwindex);
res := pchar(@dwcode);
Result := (res = '200') or (res = '302');
if assigned(hfile) then
InternetCloseHandle(hfile);
InternetCloseHandle(hSession);
end;
end;
但是,当没有Internet连接时,该函数仅在21秒后返回。那么,如何将超时限制为2秒
下面是一个测试程序:
program CheckURLTest;
{.$APPTYPE CONSOLE}
{.$R *.res}
uses
CodeSiteLogging,
wininet,
System.Types,
System.SysUtils;
function CheckUrl(url: string): boolean;
var
hSession, hfile, hRequest: hInternet;
dwindex, dwcodelen: dword;
dwcode: array [1 .. 20] of char;
res: pchar;
begin
if pos('http://', lowercase(url)) = 0 then
url := 'http://' + url;
Result := false;
hSession := InternetOpen('InetURL:/1.0', INTERNET_OPEN_TYPE_PRECONFIG,
nil, nil, 0);
if assigned(hSession) then
begin
hfile := InternetOpenUrl(hSession, pchar(url), nil, 0,
INTERNET_FLAG_RELOAD, 0);
dwindex := 0;
dwcodelen := 10;
HttpQueryInfo(hfile, HTTP_QUERY_STATUS_CODE, @dwcode, dwcodelen, dwindex);
res := pchar(@dwcode);
Result := (res = '200') or (res = '302');
if assigned(hfile) then
InternetCloseHandle(hfile);
InternetCloseHandle(hSession);
end;
end;
var
TestURL: string;
begin
try
TestURL := 'http://www.google.com';
CodeSite.Send('VOR CheckUrl');
if CheckUrl(TestURL) then
CodeSite.Send(TestURL + ' exists!')
else
CodeSite.Send(TestURL + ' does NOT exist!');
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
尝试使用设置
INTERNET\u选项\u连接\u超时
,或使用以完全控制超时。使用INTERNET设置选项
并设置接收连接超时(INTERNET\u选项\u连接\u超时
)。(由于@JoshKelley的评论,更正为使用CONNECT
而不是RECEIVE
。)
谢谢Josh,我只来了
InternetSetOption(hfile,internetoption\u CONNECT\u TIMEOUT,?)
。你能帮我了解一下其他参数吗?@user1580348-有一个完整的例子(尽管他使用了互联网选项\u接收\u超时
)-我必须测试一下,看看在实践中,你是否需要这个参数,或者互联网选项\u连接\u超时
,或者两者都需要)。他的答案在我的评论之后到达这里。@Josh:你是对的;它应该是连接的,而不是接收的。我会纠正(正确的归因)。我已经更新了你的答案,这是在我整理我的答案时发布的。在这种情况下,这是使用异步模式的正确方法吗?:hSession:=InternetOpen('InetURL:/1.0',INTERNET\u FLAG\u async,nil,nil,0)代码>肯,这一次超时超过了一分钟,然后是14秒,然后是14秒(路由器电缆断开)。@user1580348:所以在dwTimeOut
中调整超时值。我没有要测试的设置,无法从这台机器上进行测试。我在代码中留下了一条注释来解释该值应该是什么。即使使用INTERNET\u选项\u CONNECT\u TIMEOUT
该函数也只在一分钟后返回。我试着把它放在hfile:=InternetOpenUrl
之前或之后-超时总是14-18秒。文档中说的是毫秒
,这里只有一个IP地址。它必须在调用InternetOpenURL
之前,因为之后对您没有好处。:-)正如我所说,我回答了您提出的问题,即如何更改超时。我无法在您的网络连接上调试它,因为我无法访问您的网络连接。如果我的解决方案是正确的,我很困惑您为什么不接受它。
var
dwTimeOut: DWORD;
dwTimeOut := 2000; // Timeout in milliseconds
InternetSetOption(hSession, INTERNET_OPTION_CONNECT_TIMEOUT,
@dwTimeOut, SizeOf(dwTimeOut));