方法一:

#include "stdafx.h"
#include "windows.h"
#include <Sensapi.h>
#include <iostream>
#include <Wininet.h> #pragma comment(lib, "Sensapi.lib")
#pragma comment(lib, "Wininet.lib") using namespace std; int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
BOOL isConnect;
DWORD dw;
isConnect = ::IsNetworkAlive( &dw );
while ()
{
if(isConnect)
cout << "IsNetworkAlive连接" <<endl;
else
cout << "IsNetworkAlive未连接" <<endl;
cout<< "---------------------------------" <<endl; DWORD dw2;
BOOL ret = InternetGetConnectedState(&dw2, );
if (ret)
cout << "InternetGetConnectedState连接" <<endl;
else
cout << "InternetGetConnectedState未连接" <<endl;
cout<< "**********************************" <<endl; BOOL bConnected = InternetCheckConnection(_T("http://www.baidu.com"), FLAG_ICC_FORCE_CONNECTION, );
if (bConnected)
cout << "InternetCheckConnection连接" <<endl;
else
cout << "InternetCheckConnection未连接" <<endl;
cout<< "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" <<endl; //IsDestinationReachable(_T("http://www.google.com"), ) cout<<endl<<endl<<endl;
Sleep(); }
getchar();
return ;
}

方法二:

// Connect to www.baidu.com.
HINTERNET hConnect = InternetConnect(hSession,
"www.baidu.com",
INTERNET_INVALID_PORT_NUMBER,
"",
"",
INTERNET_SERVICE_HTTP,
,
); // Request the file /index.php from the server.
HINTERNET hUrl = HttpOpenRequest(hConnect,
"GET",
"/index.php",
HTTP_VERSION,
NULL,
,
INTERNET_FLAG_DONT_CACHE,
); // Add request headers
TCHAR szHeaders[] = "Accept: text/*\r\n";
BOOL bAddHeaders = HttpAddRequestHeaders(hConnect,
szHeaders,
lstrlen(szHeaders),
HTTP_ADDREQ_FLAG_ADD); // Send the request.
BOOL bSendRequest = HttpSendRequest(hUrl, NULL, , , ); if(hUrl == NULL)
{
printf("InternetOpenUrl Error......\n");
InternetCloseHandle(hSession);
return ;
} BOOL bRet = HttpQueryInfo(hUrl, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatusCode, &dwSizeOfStatusCode, NULL);
if(!bRet)
{
printf("HttpQueryInfo Error......\n");
return ;
} // Key point
if( == dwStatusCode)
{
InternetCloseHandle(hUrl) ;
InternetCloseHandle(hSession) ;
return ;
}

方法三:

//本机网络连接类型(成功)
#define NET_TYPE_RAS_DIAL_UP_CONNECT_NET 0x01 //上网类型:采用RAS拨号连接上网 0x01
#define NET_TYPE_LAN_CONNECT_NET 0x02 //上网类型:采用网卡通过局域网上网 0x02
#define NET_TYPE_PROXY_CONNECT_NET 0x04 //上网类型:使用代理服务器上网 0x04
#define NET_TYPE_RAS_INSTALL 0x10 //RAS安装 0x10 //本机网络连接(失败)
#define NET_TYPE_NO_CONNECT_NET 0x41 //未连接到网络
#define NET_STATE_VALID_CONNECT_NOCONNECT_NET 0x40 //可以联网,但当前不可用 0x40
#define NET_STATE_MODEM_BUSY 0x08 //调制解调器 繁忙 0x08
#define NET_STATE_SYSTEM_OFFLINE_MODE 0x20 //系统脱机模式 0x20 CheckNet()
{
if(!InternetGetConnectedState(&dwOnline, ))
{
if (dwOnline & INTERNET_CONNECTION_CONFIGURED )
{
return NET_STATE_VALID_CONNECT_NOCONNECT_NET;
}
if (dwOnline & INTERNET_CONNECTION_MODEM_BUSY)
{
return NET_STATE_MODEM_BUSY;
}
if (dwOnline & INTERNET_CONNECTION_OFFLINE)
{
return NET_STATE_SYSTEM_OFFLINE_MODE;
} return NET_TYPE_NO_CONNECT_NET;
} if ( dwOnline& INTERNET_CONNECTION_MODEM ) //上网类型:采用RAS拨号连接上网
{
return NET_TYPE_RAS_DIAL_UP_CONNECT_NET;
} else if ( dwOnline&INTERNET_CONNECTION_LAN ) //上网类型:采用网卡通过局域网上网
{
return NET_TYPE_LAN_CONNECT_NET;
} else if ( dwOnline& INTERNET_CONNECTION_PROXY) //在线:代理
{
return NET_TYPE_PROXY_CONNECT_NET;
} else if ( dwOnline&INTERNET_CONNECTION_MODEM_BUSY ) //MODEM被其他非INTERNET连接占用
{
return NET_TYPE_RAS_INSTALL;
}
}
05-11 09:39
查看更多