CMWAP GPRS 连接实例

来源:互联网 发布:php 维护升级 编辑:程序博客网 时间:2024/04/30 02:20

以下程序通过WAP连接Google,并获得Google网页的实例程序。

  • #include "stdafx.h"
  • #include <windows.h>
  • #include <commctrl.h>
  • #include <Wininet.h>
  • #include <Connmgr.h>
  • #include <tsp.h>
  • #include "stlogfile.h"
  • #define  GOOGLEWEBPAGE _T("/DevDivFile.xml")
  • void DestroyConnection( HANDLE hConnect )
  • {
  •         // If a connection exists, destroy it
  •         if(NULL != hConnect )
  •         {
  •                 ConnMgrReleaseConnection(hConnect, FALSE);
  •         }
  • }
  • int CreateConnection( HANDLE *phConnection )
  • {
  •         HRESULT            hResult;
  •         //        HANDLE             phConnection;
  •         DWORD              dwStatus;
  •         DWORD              dwResult;
  •         CONNMGR_DESTINATION_INFO cdi;
  •         int i;
  •         for(i = 0; SUCCEEDED(ConnMgrEnumDestinations(i, &cdi)); i++)
  •         {
  •                 if (0 == wcscmp(cdi.szDescription, _T("WAP")))
  •                         break;
  •         }
  •         hResult = ConnMgrEnumDestinations(i, &cdi);
  •         if (FAILED(hResult))
  •                 return FALSE;
  •         CONNMGR_CONNECTIONINFO ConnInfo;
  •         ZeroMemory( &ConnInfo, sizeof( ConnInfo ) );
  •         ConnInfo.cbSize = sizeof( ConnInfo );
  •         ConnInfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
  •         ConnInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
  •         ConnInfo.dwFlags = CONNMGR_FLAG_PROXY_HTTP | CONNMGR_FLAG_PROXY_WAP | CONNMGR_FLAG_PROXY_SOCKS4 | CONNMGR_FLAG_PROXY_SOCKS5;
  •         ConnInfo.bExclusive = FALSE;
  •         ConnInfo.bDisabled = FALSE;
  •         ConnInfo.guidDestNet = cdi.guid;
  •         STLOG_WRITE(_T("ConnectToHttpServer/n") );
  •         hResult = ConnMgrEstablishConnectionSync( &ConnInfo, phConnection, 60000, &dwStatus );
  •         if ( hResult != S_OK )
  •         {
  •                 STLOG_WRITE(_T("ConnMgrEstablishConnection is error/n") );
  •                 dwResult = GetLastError();
  •                 return S_FALSE;
  •         }
  •         else
  •         {
  •                 // do something
  •         }
  •         return S_OK;
  • }
  • BOOL ConnectToHttpGetServer( LPCTSTR lpszServerName, LPCTSTR lpszObjectName )
  • {
  •         BOOL   bResult;
  •         DWORD  dwResult;
  •         HANDLE hConnection;
  •         DWORD  dwWritten;
  •         LPTSTR szReadBuffer;
  •         DWORD  dwNumberOfBytesRead;
  •         int    nContentLength;
  •         DWORD  dwTotalRead;
  •         hConnection          = NULL;
  •         bResult              = FALSE;
  •         nContentLength       = 0;
  •         dwNumberOfBytesRead  = 0;
  •         dwTotalRead          = 0;
  •         dwWritten            = 0;
  •         if( CreateConnection( &hConnection ) != S_OK )
  •                 return FALSE;
  •         HINTERNET hInternet  = InternetOpen( _T("DevDiv"), INTERNET_OPEN_TYPE_PROXY, _T("10.0.0.172"), NULL, 0);
  •         if ( hInternet == NULL )
  •         {
  •                 STLOG_WRITE(_T("InternetOpen is error/n") );
  •                 dwResult = GetLastError();
  •                 return bResult;
  •         }
  •         HINTERNET hInternetConnect = InternetConnect( hInternet, lpszServerName, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1 );
  •         if ( hInternetConnect == NULL )
  •         {
  •                 STLOG_WRITE(_T("InternetConnect is error/n") );
  •                 dwResult = GetLastError();
  •                 return bResult;
  •         }
  •         //TCHAR   szAccept[]   =   L"*/*";   //we   accept   everything...
  •         //LPTSTR  AcceptTypes[2]={0};
  •         //AcceptTypes[0]=szAccept;
  •         HINTERNET hRequest = HttpOpenRequest(hInternetConnect,_T("GET"),lpszObjectName, L"HTTP/1.1",NULL,(LPCTSTR *)NULL,0,1);
  •         if   (!hRequest)
  •         {
  •                 dwResult = GetLastError();
  •                 STLOG_WRITE(_T("HttpOpenRequest is error/n") );
  •                 return bResult;
  •         }
  •         BOOL bSuccess = HttpSendRequest(hRequest, NULL, 0, NULL, 0 );
  •         if ( !bSuccess )
  •         {
  •                 STLOG_WRITE(_T("HttpSendRequestEx is error/n") );
  •                 dwResult = GetLastError();
  •                 return bResult;
  •         }
  •         TCHAR szHeader[ 1000 ];
  •         ZeroMemory( szHeader, sizeof( szHeader ) );
  •         DWORD dwHeaderLength = sizeof( szHeader );
  •         // HTTP_QUERY_RAW_HEADERS
  •         BOOL bQuery=HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH, szHeader, &dwHeaderLength, NULL);
  •         if ( !bQuery )
  •         {
  •                 STLOG_WRITE(_T("HttpQueryInfo is error/n") );
  •                 dwResult = GetLastError();
  •                 return bResult;
  •         }
  •         nContentLength = _wtoi( szHeader );
  •         HANDLE        hFile = CreateFile( GOOGLEWEBPAGE, GENERIC_READ|GENERIC_WRITE, 0, NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
  •         if ( hFile == INVALID_HANDLE_VALUE )
  •         {
  •                 dwResult = GetLastError();
  •                 return bResult;
  •         }
  •         szReadBuffer = (LPTSTR) malloc( 1024 );
  •         ZeroMemory(szReadBuffer, 1024 );
  •         while( TRUE )
  •         {
  •                 bResult = InternetReadFile(hRequest, szReadBuffer, 1024, &dwNumberOfBytesRead);
  •                 if ( bResult )
  •                 {
  •                         if ( dwTotalRead == nContentLength && dwNumberOfBytesRead == 0 )
  •                         {
  •                                 bResult = TRUE;
  •                                 break;
  •                         }
  •                         else
  •                         {
  •                                 SetFilePointer ( hFile, 0, NULL, FILE_END );
  •                                 WriteFile( hFile,(LPBYTE)szReadBuffer, dwNumberOfBytesRead, &dwWritten, NULL );
  •                                 dwTotalRead = dwTotalRead + dwNumberOfBytesRead;
  •                         }
  •                 }
  •                 else
  •                 {
  •                         bResult = FALSE;
  •                         break;
  •                 }
  •         }
  •         CloseHandle(hFile);
  •         free(szReadBuffer);
  •         InternetCloseHandle( hInternetConnect );
  •         InternetCloseHandle( hInternet );
  •         DestroyConnection( hConnection );
  •         STLOG_WRITE(_T("www.devdiv.net/n") );
  •         return bResult;
  • }
  • int _tmain(int argc, _TCHAR* argv[])
  • {
  •         ConnectToHttpGetServer( _T("www.google.cn"), NULL );
  •         return 0;
  • }