windows mobile 网络连接示例 2008-03-25 15:17
移动设备的网络应用,首先要管理网络连接,通常就是拨号连接GPRS,然后进行网络操作,一般可以建立socket应用或者使用wininet建立web应用
==========================================
管理网络连接:在未连接上时进行自动连接
#include <initguid.h>//为了使用IID_DestNetInternet,需要在connmgr.h之前,且一个项目只能引用一次
#include <connmgr.h>
#pragma comment( lib, "cellcore.lib" )
#include <winsock2.h>
#pragma comment( lib, "Ws2.lib" )
// It is good practice to load the cellcore.dll
// dynamically to be able to compile the code even for older platforms
HINSTANCE hcellDll = LoadLibrary(TEXT("cellcore.dll"));
if (hcellDll)
{
// Here comes the main code:
// First we check if we might have a connection
DWORD pdwStatus;
ConnMgrConnectionStatus(&phWebConnection,&pdwStatus);
if (pdwStatus==CONNMGR_STATUS_CONNECTED)
{
//We are already connected!
//This code should never run since we should
//have a valid IP already.
//If you still get here, you probably have
//stale connection.
}
else
{
//We are not connected, so lets try:
//The CONNECTIONINFO is the structure that
//tells Connection Manager how we want
//to connect
CONNMGR_CONNECTIONINFO sConInfo;
memset(&sConInfo,0, sizeof(CONNMGR_CONNECTIONINFO));
sConInfo.cbSize=sizeof(CONNMGR_CONNECTIONINFO);
// We want to use the 揼uidDestNet?parameter
sConInfo.dwParams=CONNMGR_PARAM_GUIDDESTNET;
// This is the highest data priority.
sConInfo.dwPriority=CONNMGR_PRIORITY_USERINTERACTIVE;
sConInfo.dwFlags=0;
// Lets be nice and share the connection with
// other applications
sConInfo.bExclusive=FALSE;
sConInfo.bDisabled=FALSE;
sConInfo.guidDestNet=IID_DestNetInternet;
// We want to wait until the connection was
// established successful but not longer then
// 60 seconds. You can use
// ConnMgrEstablishConnection to establish
// an asynchronous connection.
if (ConnMgrEstablishConnectionSync(&sConInfo,&phWebConnection,60000,&pdwStatus)==S_OK)
{
//We are successfully connected!
//Now lets try to get the new IP address?
tried2Connect=TRUE;
}
else
{
tried2Connect=FALSE;
//Doh! Connection failed!
}
}
}
============================================
下面是一个winnet的应用,基本上windows mobile版和windows版是差不多的,但手机版必须使用unicode
TCHAR hdrs[] = _T("Content-Type: application/x-www-form-urlencoded");
TCHAR frmdata[] = _T(""); //请求的参数串
TCHAR accept[] = _T("Accept: */*");
char ret[64];
TCHAR reData[64] ;
DWORD reLength;
HINTERNET hSession = InternetOpen(L"Mobile",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if( hSession == NULL )
MessageBox(NULL,TEXT("InternetOpen"),TEXT("test"),MB_OK);
HINTERNET hConnect = InternetConnect(hSession, _T("gis.edushi.com"),80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
if( hConnect == NULL )
MessageBox(NULL,TEXT("InternetConnect"),TEXT("test"),MB_OK);
HINTERNET hRequest = HttpOpenRequest(hConnect, L"POST",_T("/MobileTest/mSvr.cgi"), NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
if( hRequest == NULL )
MessageBox(NULL,TEXT("HttpOpenRequest"),TEXT("test"),MB_OK);
//发送http请求
if( HttpSendRequest(hRequest, hdrs, wcslen(hdrs), frmdata, wcslen(frmdata))==FALSE )
MessageBox(NULL,TEXT("HttpSendRequest"),TEXT("test"),MB_OK);
//HttpQueryInfo(hRequest, HTTP_QUERY_ACCEPT_CHARSET | HTTP_QUERY_STATUS_CODE, &dwStatus, &dwStatusSize, NULL);
//读取返回结果
if( InternetReadFile(hRequest,ret,64,&reLength )==FALSE )
MessageBox(NULL,TEXT("InternetReadFile"),TEXT("test"),MB_OK);
else
{//ansi字符串转换到unicode
int nSize = MultiByteToWideChar(CP_ACP,0,ret,-1,NULL,0);
MultiByteToWideChar(CP_ACP,0,ret,-1,reData,nSize);
MessageBox(NULL,reData,TEXT("test"),MB_OK);
}
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);