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;
- }
- CMWAP GPRS 连接实例
- GPRS网络连接类型(cmwap/cmnet)程序判断
- GPRS cmwap cmnet
- 使用GPRS的cmwap接入点实现网络通讯1-----建立连接
- GPRS模式CMWAP和CMNET的比较
- 什么是GPRS,CMWAP,CMNET-移动网络介绍
- GPRS、WAP、WAP网关、HTTP、cmwap,、cmnet
- Android 使用cmwap GPRS 方式联网
- GPRS连接
- CMNET和CMWAP网络连接
- CMNET和CMWAP网络连接
- CMNET和CMWAP网络连接
- CMNET和CMWAP网络连接
- CMNET和CMWAP网络连接
- CMNET和CMWAP网络连接
- gsm,gprs,cmwap,cmnet,3g,TD-SCDMA,CDMA2000,WCDMA
- 四讲:关于 CMWAP,CMNET,GPRS,EDGE 问题集合
- gsm,gprs,cmwap,cmnet,3g,TD-SCDMA,CDMA2000,WCDMA
- SQL SERVER 表及索引分区
- cmwap和cmnet接入点区别
- SQL SERVER 2008的数据压缩
- Backtraces引起的内存泄露
- 调用远程数据库的函数
- CMWAP GPRS 连接实例
- main()与main(int argc,char *argv[])
- 关于qq号为83460704此等小人心态的记录
- 让SQL SERVER突破2G内存限制
- c# GDI+简单绘图(一)
- c# GDI+简单绘图(二)
- 练习2.5.。练习2.6
- 喜欢嵌入式Linux的 请进来
- C# GDI+ 简单绘图 (三)