코딩하는 나귀

서버 상태 확인

델파이2009. 1. 12. 17:39

개발을 하다보면 서버가 살아 있는지 확인 해야할 필요가 있다.
보통은 연결이 살아있는지 확인 하는 것이지만...
난... 그냥 HTTP서버가 잘 살아 있는지만 확인하고 싶었을 뿐이고!
그래서 뒤지다 아래같은 함수를 찿았을 뿐이고!

function IsConnected(URL: String): Boolean;
const
  // Local system has a valid connection to the Internet, but it might or might
  // not be currently connected.
  INTERNET_CONNECTION_CONFIGURED = $40;

  // Local system uses a local area network to connect to the Internet.
  INTERNET_CONNECTION_LAN = $02;

  // Local system uses a modem to connect to the Internet
  INTERNET_CONNECTION_MODEM = $01;

  // Local system is in offline mode.
  INTERNET_CONNECTION_OFFLINE = $20;

  // Local system uses a proxy server to connect to the Internet
  INTERNET_CONNECTION_PROXY = $04;

  // Local system has RAS installed.
  INTERNET_RAS_INSTALLED = $10;

var
  InetState: DWORD;
  hHttpSession, hReqUrl: HInternet;

begin
  Result := InternetGetConnectedState(@InetState, 0);
  if (Result and (InetState and INTERNET_CONNECTION_CONFIGURED = INTERNET_CONNECTION_CONFIGURED) ) then begin
    // so far we ONLY know there's a valid connection. See if we can grab some known URL ...
    hHttpSession := InternetOpen(
                      PChar(Application.Title),
                      INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
    try
      hReqUrl := InternetOpenURL(hHttpSession, URL, nil, 0,0,0);
      Result := hReqUrl <> nil;
      InternetCloseHandle(hReqUrl);
    finally
      InternetCloseHandle(hHttpSession);
    end;
  end
  else
    if (InetState and INTERNET_CONNECTION_OFFLINE = INTERNET_CONNECTION_OFFLINE) then
      Result := False; // we know for sure we are offline.
end;