中断 HTTP GET 操作
Interrupt HTTP GET operation
在一种特殊的诊断模式下,我通过在已知的始终预期在线的服务器上执行 HTTP GET 操作来测试 Internet 连接。虽然这通常非常快,但在远程位置工作或禁用计算机网络适配器时,这可能会很慢 and/or 15 秒或更长时间后超时。
是否可以配置 Indy 组件以便我可以按需中断它?或者也许有更好的方法来执行测试 HTTP GET 操作?我的代码在下面,我可能会用 HasInternet(http://master11.teamviewer.com/);
调用
function HasInternet(strTestWebServer: String) : Boolean;
var
idHTTP: TIdHTTP;
begin
// Test an internet connection to the named server
Result := False;
idHTTP := TIdHTTP.Create(nil);
if (idHTTP <> nil) then
begin
try
try
// Handle redirects in response from HTTP server. Not required for some servers,
// but can confirm a connection to servers that redirect to https.
idHTTP.HandleRedirects := True;
// Add in the HTTP protocol header (if required) and retrieve the HTTP resource
// Note: HTTP_PROTOCOL = 'http://';
if (AnsiPos(HTTP_PROTOCOL, strTestWebServer) > 0) then
Result := (idHTTP.Get(strTestWebServer) <> '')
else
Result := (idHTTP.Get(HTTP_PROTOCOL + strTestWebServer) <> '');
except
// HTTP protocol errors are sometimes returned by servers, with the status code
// saved in TIdHTTP.ResponseCode. Common responses include:
// * 403 Forbidden (request was valid, but server is refusing to respond to it)
// * 404 Not Found (requested resource could not be found)
// * 405 Method Not Allowed (requested method not supported by that resource)
// These errors are counted as "valid connection to the internet possible"
on E: EIdHTTPProtocolException do
// Status code can be found in "E.ReplyErrorCode" or "idHTTP.ResponseCode"
Result := True;
else
// All other exceptions are counted as definite failures
Result := False;
end;
finally
idHTTP.Free();
end;
end;
end;
您可以通过某种方式打断 运行 TIdHTTP HTTP method by calling Disconnect from context of a thread different from the one in which the method was executed. With your design it would mean exposing the internally used TIdHTTP 对象。
但是对于您的任务,您可能会给出例如InternetCheckConnection 功能一试。例如 Delphi 7 可以是:
const
FLAG_ICC_FORCE_CONNECTION = [=10=]000001;
function InternetCheckConnectionA(lpszUrl: PAnsiChar; dwFlags: DWORD; dwReserved: DWORD): BOOL; stdcall;
external 'wininet.dll' name 'InternetCheckConnectionA';
function InternetCanConnect(const URL: AnsiString): Boolean;
begin
Result := Boolean(InternetCheckConnectionA(PAnsiChar(URL), FLAG_ICC_FORCE_CONNECTION, 0));
end;
function InternetNotConnected(const URL: AnsiString): Boolean;
begin
Result := not Boolean(InternetCheckConnectionA(PAnsiChar(URL),
FLAG_ICC_FORCE_CONNECTION, 0)) and (GetLastError = ERROR_NOT_CONNECTED);
end;
在一种特殊的诊断模式下,我通过在已知的始终预期在线的服务器上执行 HTTP GET 操作来测试 Internet 连接。虽然这通常非常快,但在远程位置工作或禁用计算机网络适配器时,这可能会很慢 and/or 15 秒或更长时间后超时。
是否可以配置 Indy 组件以便我可以按需中断它?或者也许有更好的方法来执行测试 HTTP GET 操作?我的代码在下面,我可能会用 HasInternet(http://master11.teamviewer.com/);
function HasInternet(strTestWebServer: String) : Boolean;
var
idHTTP: TIdHTTP;
begin
// Test an internet connection to the named server
Result := False;
idHTTP := TIdHTTP.Create(nil);
if (idHTTP <> nil) then
begin
try
try
// Handle redirects in response from HTTP server. Not required for some servers,
// but can confirm a connection to servers that redirect to https.
idHTTP.HandleRedirects := True;
// Add in the HTTP protocol header (if required) and retrieve the HTTP resource
// Note: HTTP_PROTOCOL = 'http://';
if (AnsiPos(HTTP_PROTOCOL, strTestWebServer) > 0) then
Result := (idHTTP.Get(strTestWebServer) <> '')
else
Result := (idHTTP.Get(HTTP_PROTOCOL + strTestWebServer) <> '');
except
// HTTP protocol errors are sometimes returned by servers, with the status code
// saved in TIdHTTP.ResponseCode. Common responses include:
// * 403 Forbidden (request was valid, but server is refusing to respond to it)
// * 404 Not Found (requested resource could not be found)
// * 405 Method Not Allowed (requested method not supported by that resource)
// These errors are counted as "valid connection to the internet possible"
on E: EIdHTTPProtocolException do
// Status code can be found in "E.ReplyErrorCode" or "idHTTP.ResponseCode"
Result := True;
else
// All other exceptions are counted as definite failures
Result := False;
end;
finally
idHTTP.Free();
end;
end;
end;
您可以通过某种方式打断 运行 TIdHTTP HTTP method by calling Disconnect from context of a thread different from the one in which the method was executed. With your design it would mean exposing the internally used TIdHTTP 对象。
但是对于您的任务,您可能会给出例如InternetCheckConnection 功能一试。例如 Delphi 7 可以是:
const
FLAG_ICC_FORCE_CONNECTION = [=10=]000001;
function InternetCheckConnectionA(lpszUrl: PAnsiChar; dwFlags: DWORD; dwReserved: DWORD): BOOL; stdcall;
external 'wininet.dll' name 'InternetCheckConnectionA';
function InternetCanConnect(const URL: AnsiString): Boolean;
begin
Result := Boolean(InternetCheckConnectionA(PAnsiChar(URL), FLAG_ICC_FORCE_CONNECTION, 0));
end;
function InternetNotConnected(const URL: AnsiString): Boolean;
begin
Result := not Boolean(InternetCheckConnectionA(PAnsiChar(URL),
FLAG_ICC_FORCE_CONNECTION, 0)) and (GetLastError = ERROR_NOT_CONNECTED);
end;