首先我用:
dwConnectionTypes:= MODEM + LAN + PROXY;
Result:=InternetGetConnectedState(@dwConnectionTypes,0);判断网络连接是否正常如果正常我再用 InternetCheckConnection('服务地址',1,0); 判断能否访问指定的服务地址()现在问题来了,客户有可能是通过IE代理来上外网,而InternetCheckConnection 判断好像是不通过代理的所以 InternetCheckConnection('服务地址',1,0) 一直反回false给我,实际用IE是可以打开服务地址的请问各位是否有什么解决办法来判断 我这个服务地址是通的,在有代理和没代理下都能正确判断出来项目快完成了,这估计是最后一个问题了,大家一定要给力哈~~~~~~~~~~~~~
dwConnectionTypes:= MODEM + LAN + PROXY;
Result:=InternetGetConnectedState(@dwConnectionTypes,0);判断网络连接是否正常如果正常我再用 InternetCheckConnection('服务地址',1,0); 判断能否访问指定的服务地址()现在问题来了,客户有可能是通过IE代理来上外网,而InternetCheckConnection 判断好像是不通过代理的所以 InternetCheckConnection('服务地址',1,0) 一直反回false给我,实际用IE是可以打开服务地址的请问各位是否有什么解决办法来判断 我这个服务地址是通的,在有代理和没代理下都能正确判断出来项目快完成了,这估计是最后一个问题了,大家一定要给力哈~~~~~~~~~~~~~
Execute: 调用我就好了,其它的交给我去办
属性
Busy: 我正忙着呢,还没检测完,一会儿再来
AccessType: 你希望我怎么连接网络,直接连还是用代理?
AsynRequest: 同步检测的话我们要有一会儿看不到对方了,呜呜
UserAgent: 想告诉Web服务器是阿猫还是阿狗在连接?在这里设置就好了
Proxy: 让我用代理连网络,用哪个代理啊?
Url: 原来是要检测这个地址能不能访问,保证办到
事件
OnStart: 开始检测之前我会在这里通知你一声
OnStatusChange:检测过程中发生什么事我会在这里及时通知你的
OnComplete: 检测完成了,成功不成功我都会告诉你
{**********************************************************}
{ }
{ 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.
我用InternetOpenUrl 来判断是否能打开我的webservice是可以测试成功的,但有一个问题是:如果我在我的程序里通过代码修改了IE的代理服务器(就是修改注册表)
然后再接着测试连接,却出现超时(按照预计应该是成功的),我把程序关掉重起后,再去测试访问我的webservice地址是可以的,,这是怎么回事,怎么通过代码修改IE代理后会超时啊超时啊
超时啊
超时啊
各位知道麻烦指点一下咯