VC:检测网络连接的方法

 方法一:

#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/*\r\n";
      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......\n");
              InternetCloseHandle(hSession);
              return 0;
      }
       
      BOOL bRet = HttpQueryInfo(hUrl, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatusCode, &dwSizeOfStatusCode, NULL);
      if(!bRet)
      {
             printf("HttpQueryInfo Error......\n");
             return 0;
      }
       
      // Key point
      if(404 == dwStatusCode)
      {
             InternetCloseHandle(hUrl) ;
             InternetCloseHandle(hSession) ;
             return 0;
      }

猜你喜欢

转载自www.cnblogs.com/2018shawn/p/11810349.html