关键词:InternetOpen, InternetSetStatusCallback, InternetOpenUrl, INTERNET_STATUS_CALLBACK
{**********************************************************}
{ }
{ TZoCInetChecker Component Version 1.00 }
{ }
{ Function: Asynchronously Check if a given web page }
{ can be successfully retreived and feed back }
{ the user with callback status infomation }
{ }
{ }
{ This is a freeware. If you made cool changes on it, }
{ please send them to me. }
{ }
{ Email: [email protected] }
{ URL: http://www.ZoCsoft.com }
{ }
{ History: + New Feature, - Removed, * Modified }
{ }
{ version 1.00 2005-06-03 }
{ The first version }
{ }
{**********************************************************}
unit ZoCInetChecker;
interface
uses
Windows, Messages, SysUtils, Classes, WinInet;
const
INTERNET_STATUS_DETECTING_PROXY = 80;
{$EXTERNALSYM INTERNET_STATUS_DETECTING_PROXY}
WM_STATUSCHANGE = WM_USER + 200;
WM_CHECKCOMPLETE = WM_USER + 201;
type
LPINTERNET_ASYNC_RESULT = ^INTERNET_ASYNC_RESULT;
INTERNET_ASYNC_RESULT = record
dwResult: DWORD;
dwError: DWORD;
end;
pREQUEST_CONTEXT = ^REQUEST_CONTEXT;
REQUEST_CONTEXT = record
hWindow: HWND;
hOpen: HINTERNET; //HINTERNET handle created by InternetOpen
end;
TOnStatusChangeEvent = procedure(Sender: TObject; StatusCode: Cardinal) of object;
TOnCompleteEvent = procedure(Sender: TObject; Connected: Boolean) of object;
TAccessType = (atDirectConnect, atPreConfig, atPreConfigWithNoProxy, atProxy);
TZoCInetChecker = class(TComponent)
private
{ Private declarations }
FUrl: string;
FAccessType: TAccessType;
FProxy: string;
FOnStart: TNotifyEvent;
FOnStatusChange: TOnStatusChangeEvent;
FOnComplete: TOnCompleteEvent;
FBusy: Boolean;
hOpenUrl, hOpen: HINTERNET;
FUserAgent: string;
FWindowHandle: HWnd;
iscCallback: INTERNET_STATUS_CALLBACK;
RC: REQUEST_CONTEXT;
FAsynRequest: Boolean;
procedure WndProc(var Msg: TMessage);
protected
{ Protected declarations }
procedure DoOnStatusChange(StatusCode: Cardinal); dynamic;
public
{ Public declarations }
property Busy: Boolean read FBusy write FBusy;
function Execute: Boolean;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property AccessType: TAccessType read FAccessType write FAccessType
default atDirectConnect;
property AsynRequest: Boolean read FAsynRequest write FAsynRequest
default True;
property UserAgent: string read FUserAgent write FUserAgent;
property Proxy: string read FProxy write FProxy;
property Url: string read FUrl write FUrl;
property OnStart: TNotifyEvent read FOnStart write FOnStart;
property OnStatusChange: TOnStatusChangeEvent read FOnStatusChange write
FOnStatusChange;
property OnComplete: TOnCompleteEvent read FOnComplete write FOnComplete;
end;
procedure Register;
function StatusCode2StatusText(StatusCode: Cardinal): string;
implementation
procedure Register;
begin
RegisterComponents('ZoC', [TZoCInetChecker]);
end;
{ TZoCInetChecker }
procedure InternetCallback(hInternet: HINTERNET; dwContext: DWORD;
dwInternetStatus: DWORD; lpvStatusInformation: hwnd;
dwStatusInformationLength: DWORD); stdcall;
var
cpContext : pREQUEST_CONTEXT;
FConnected : Boolean;
dwindex, dwcodelen : dword;
dwcode : array[1..20] of char;
res : pchar;
begin
cpContext := pREQUEST_CONTEXT(dwContext);
PostMessage(cpContext^.hWindow, WM_STATUSCHANGE, dwInternetStatus, 0);
if dwInternetStatus = INTERNET_STATUS_REQUEST_COMPLETE then
begin
dwIndex := 0;
dwCodeLen := 10;
HttpQueryInfo(Pointer(LPINTERNET_ASYNC_RESULT(lpvStatusInformation).dwResult),
HTTP_QUERY_STATUS_CODE, @dwcode, dwcodeLen, dwIndex);
res := pchar(@dwcode);
//HTTP_STATUS_OK 200 The request completed successfully
//HTTP_STATUS_REDIRECT 302 The requested resource resides temporarily under a different URI
FConnected := (res = '200') or (res = '302');
InternetCloseHandle(Pointer(LPINTERNET_ASYNC_RESULT(lpvStatusInformation).dwResult));
InternetCloseHandle(cpContext^.hOpen);
PostMessage(cpContext^.hWindow, WM_CHECKCOMPLETE, Integer(FConnected), 0);
end;
end;
constructor TZoCInetChecker.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FWindowHandle := AllocateHWnd(WndProc);
FAccessType := atDirectConnect;
FAsynRequest := True;
FUserAgent := 'ZoCSoft Internet Checker 1.0';
end;
destructor TZoCInetChecker.Destroy;
begin
InternetCloseHandle(hOpenUrl);
if FWindowHandle <> 0 then
DeallocateHWnd(FWindowHandle);
inherited Destroy;
end;
procedure TZoCInetChecker.DoOnStatusChange(StatusCode: Cardinal);
begin
if Assigned(FOnStatusChange) then
FOnStatusChange(Self, StatusCode);
end;
function TZoCInetChecker.Execute: Boolean;
var
Flags : Cardinal;
begin
if FUrl = '' then
Exit;
FBusy := True;
if Assigned(FOnStart) then
FOnStart(Self);
if FAsynRequest then
Flags := INTERNET_FLAG_ASYNC
else
Flags := 0;
case FAccessType of
atDirectConnect:
hOpen := InternetOpen(PChar(FUserAgent), INTERNET_OPEN_TYPE_DIRECT,
nil, nil, Flags);
atPreConfig:
hOpen := InternetOpen(PChar(FUserAgent), INTERNET_OPEN_TYPE_PRECONFIG,
nil, nil, Flags);
atPreConfigWithNoProxy:
hOpen := InternetOpen(PChar(FUserAgent),
INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY,
nil, nil, Flags);
atProxy:
hOpen := InternetOpen(PChar(FUserAgent), INTERNET_OPEN_TYPE_PROXY,
PChar(FProxy), nil, Flags);
end;
RC.hWindow := FWindowHandle;
RC.hOpen := hOpen;
if FAsynRequest then
iscCallback := InternetSetStatusCallback(hOpen,
INTERNET_STATUS_CALLBACK(@InternetCallback));
hOpenUrl := InternetOpenUrl(hOpen, PChar(FUrl), nil, 0,
INTERNET_FLAG_RELOAD, Cardinal(@RC));
Result := hOpenUrl <> nil;
end;
procedure TZoCInetChecker.WndProc(var Msg: TMessage);
begin
case Msg.Msg of
WM_STATUSCHANGE:
DoOnStatusChange(Msg.WParam);
WM_CHECKCOMPLETE:
begin
FBusy := True;
if Assigned(FOnComplete) then
FOnComplete(Self, Boolean(Msg.WParam));
end;
else
Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);
end;
end;
function StatusCode2StatusText(StatusCode: Cardinal): string;
begin
case StatusCode of
INTERNET_STATUS_CLOSING_CONNECTION:
Result := 'Closing connection to the server.';
INTERNET_STATUS_CONNECTED_TO_SERVER:
Result := 'Successfully connected to the socket address.';
INTERNET_STATUS_CONNECTING_TO_SERVER:
Result := 'Connecting to the socket address.';
INTERNET_STATUS_CONNECTION_CLOSED:
Result := 'Closed the connection to the server.';
INTERNET_STATUS_DETECTING_PROXY:
Result := 'Proxy has been detected.';
INTERNET_STATUS_HANDLE_CLOSING:
Result := 'Handle value has been terminated.';
INTERNET_STATUS_HANDLE_CREATED:
Result := 'InternetConnect has created a new handle.';
INTERNET_STATUS_INTERMEDIATE_RESPONSE:
Result := 'Received status code message from the server.';
INTERNET_STATUS_NAME_RESOLVED:
Result := 'Successfully found the IP address.';
INTERNET_STATUS_RECEIVING_RESPONSE:
Result := 'Waiting for the server to respond.';
INTERNET_STATUS_REDIRECT:
Result := 'Request is about to be redirected.';
INTERNET_STATUS_REQUEST_COMPLETE:
Result := 'Completed.';
INTERNET_STATUS_REQUEST_SENT:
Result := 'Successfully sent the information request to the server.';
INTERNET_STATUS_RESOLVING_NAME:
Result := 'Looking up the IP address of the name.';
INTERNET_STATUS_RESPONSE_RECEIVED:
Result := 'Successfully received a response from the server.';
INTERNET_STATUS_SENDING_REQUEST:
Result := 'Sending the information request to the server.';
INTERNET_STATUS_STATE_CHANGE:
Result := 'Security State Change.';
else
Result := 'Unknown Status.';
end;
end;
end.