方法一:
#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 (1) { if(isConnect) cout << "IsNetworkAlive连接" <<endl; else cout << "IsNetworkAlive未连接" <<endl; cout<< "---------------------------------" <<endl; DWORD dw2; BOOL ret = InternetGetConnectedState(&dw2, 0); if (ret) cout << "InternetGetConnectedState连接" <<endl; else cout << "InternetGetConnectedState未连接" <<endl; cout<< "**********************************" <<endl; BOOL bConnected = InternetCheckConnection(_T("http://www.baidu.com"), FLAG_ICC_FORCE_CONNECTION, 0); if (bConnected) cout << "InternetCheckConnection连接" <<endl; else cout << "InternetCheckConnection未连接" <<endl; cout<< "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" <<endl; //IsDestinationReachable(_T("http://www.google.com"), ) cout<<endl<<endl<<endl; Sleep(1000); } getchar(); return 1; }
方法二:
// Connect to www.baidu.com. HINTERNET hConnect = InternetConnect(hSession, "www.baidu.com", INTERNET_INVALID_PORT_NUMBER, "", "", INTERNET_SERVICE_HTTP, 0, 0); // Request the file /index.php from the server. HINTERNET hUrl = HttpOpenRequest(hConnect, "GET", "/index.php", HTTP_VERSION, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0); // Add request headers TCHAR szHeaders[] = "Accept: text/* "; BOOL bAddHeaders = HttpAddRequestHeaders(hConnect, szHeaders, lstrlen(szHeaders), HTTP_ADDREQ_FLAG_ADD); // Send the request. BOOL bSendRequest = HttpSendRequest(hUrl, NULL, 0, 0, 0); if(hUrl == NULL) { printf("InternetOpenUrl Error...... "); InternetCloseHandle(hSession); return 0; } BOOL bRet = HttpQueryInfo(hUrl, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatusCode, &dwSizeOfStatusCode, NULL); if(!bRet) { printf("HttpQueryInfo Error...... "); return 0; } // Key point if(404 == dwStatusCode) { InternetCloseHandle(hUrl) ; InternetCloseHandle(hSession) ; return 0; }
方法三:
//本机网络连接类型(成功) #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, 0)) { 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; } }