法甲

TZoCInetChecker:检测网络连接的类 2

2019-09-12 17:02:02来源:励志吧0次阅读

{TZoCInetChecker}

procedureInternetCallback(hInternet:HINTERNET;dwContext:DWORD;

dwInternetStatus:DWORD;lpvStatusInformation:hwnd;

dwStatusInformationLength:DWORD);stdcall;

var

cpContext:pREQUEST_CONTEXT;

FConnected:Boolean;

dwindex,dwcodelen:dword;

dwcode:array[1..20]ofchar;

res:pchar;

begin

cpContext:=pREQUEST_CONTEXT(dwContext);

PostMessage(cpContext^.hWindow,WM_STATUSCHANGE,dwInternetStatus,0);

ifdwInternetStatus=INTERNET_STATUS_REQUEST_COMPLETEthen

begin

dwIndex:=0;

dwCodeLen:=10;

HttpQueryInfo(Pointer(LPINTERNET_ASYNC_RESULT(lpvStatusInformation).dwResult),

HTTP_QUERY_STATUS_CODE,@dwcode,dwcodeLen,dwIndex);

res:=pchar(@dwcode);

//HTTP_STATUS_OK200Therequestcompletedsuccessfully

//HTTP_STATUS_REDIRECT302TherequestedresourceresidestemporarilyunderadifferentURI

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;

constructorTZoCInetChecker.Create(AOwner:TComponent);

begin

inheritedCreate(AOwner);

FWindowHandle:=AllocateHWnd(WndProc);

FAccessType:=atDirectConnect;

FAsynRequest:=True;

FUserAgent:='ZoCSoftInternetChecker1.0';

end;

destructorTZoCInetChecker.Destroy;

begin

InternetCloseHandle(hOpenUrl);

ifFWindowHandle<>0then

DeallocateHWnd(FWindowHandle);

inheritedDestroy;

end;

procedureTZoCInetChecker.DoOnStatusChange(StatusCode:Cardinal);

begin

ifAssigned(FOnStatusChange)then

FOnStatusChange(Self,StatusCode);

end;

functionTZoCInetChecker.Execute:Boolean;

var

Flags:Cardinal;

begin

ifFUrl=''then

Exit;

FBusy:=True;

ifAssigned(FOnStart)then

FOnStart(Self);

ifFAsynRequestthen

Flags:=INTERNET_FLAG_ASYNC

else

Flags:=0;

caseFAccessTypeof

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;

ifFAsynRequestthen

iscCallback:=InternetSetStatusCallback(hOpen,

INTERNET_STATUS_CALLBACK(@InternetCallback));

hOpenUrl:=InternetOpenUrl(hOpen,PChar(FUrl),nil,0,

INTERNET_FLAG_RELOAD,Cardinal(@RC));

Result:=hOpenUrl<>nil;

end;

procedureTZoCInetChecker.WndProc(varMsg:TMessage);

begin

caseMsg.Msgof

WM_STATUSCHANGE:

DoOnStatusChange(Msg.WParam);

WM_CHECKCOMPLETE:

begin

FBusy:=True;

ifAssigned(FOnComplete)then

FOnComplete(Self,Boolean(Msg.WParam));

end;

else

Msg.Result:=DefWindowProc(FWindowHandle,Msg.Msg,Msg.wParam,Msg.lParam);

end;

end;

functionStatusCode2StatusText(StatusCode:Cardinal):string;

begin

caseStatusCodeof

INTERNET_STATUS_CLOSING_CONNECTION:

Result:='Closingconnectiontotheserver.';

INTERNET_STATUS_CONNECTED_TO_SERVER:

Result:='Successfullyconnectedtothesocketaddress.';

INTERNET_STATUS_CONNECTING_TO_SERVER:

Result:='Connectingtothesocketaddress.';

INTERNET_STATUS_CONNECTION_CLOSED:

Result:='Closedtheconnectiontotheserver.';

INTERNET_STATUS_DETECTING_PROXY:

Result:='Proxyhasbeendetected.';

INTERNET_STATUS_HANDLE_CLOSING:

Result:='Handlevaluehasbeenterminated.';

INTERNET_STATUS_HANDLE_CREATED:

Result:='InternetConnecthascreatedanewhandle.';

INTERNET_STATUS_INTERMEDIATE_RESPONSE:

Result:='Receivedstatuscodemessagefromtheserver.';

INTERNET_STATUS_NAME_RESOLVED:

Result:='SuccessfullyfoundtheIPaddress.';

INTERNET_STATUS_RECEIVING_RESPONSE:

Result:='Waitingfortheservertorespond.';

INTERNET_STATUS_REDIRECT:

Result:='Requestisabouttoberedirected.';

INTERNET_STATUS_REQUEST_COMPLETE:

Result:='Completed.';

INTERNET_STATUS_REQUEST_SENT:

Result:='Successfullysenttheinformationrequesttotheserver.';

INTERNET_STATUS_RESOLVING_NAME:

Result:='LookinguptheIPaddressofthename.';

INTERNET_STATUS_RESPONSE_RECEIVED:

Result:='Successfullyreceivedaresponsefromtheserver.';

INTERNET_STATUS_SENDING_REQUEST:

Result:='Sendingtheinformationrequesttotheserver.';

INTERNET_STATUS_STATE_CHANGE:

Result:='SecurityStateChange.';

else

Result:='UnknownStatus.';

end;

end;

end.

4、后记

不出所料,网络小灵通果然不负众望得到不少同学的支持,大家也不用坐在计算机前无聊地看网络是否恢复了,因为它不仅能检测内网是否连通,也能检测外网是否能上,在检测成功后它会播放自定义的声音通知,为此我还专门找寝室一个兄弟录制了两段粗犷的“通网了”/“断网了”音频为大家解闷;再后来还加入了查找本网段内空闲IP和检查哪个IP可能在搞ARP欺骗的功能,直到学校的网络秩序逐渐转好,网络状况也慢慢好转,网络小灵通才回到了我的Code Repository里面,安睡了。

5个月婴儿发烧如何退烧
小孩脸色发黄
糖尿病胃轻瘫便秘如何治疗
婴儿手心出汗
分享到: