星期四, 7月 23, 2009

Windows Mobile GPRS Connection via Proxy (Wap Gateway)

在Windows Mobile中,使用win32 api連接至internet,首先必須建立APN連線,再依下列步驟即可,最後送出一個query request,返回200則表示連線成功,網路上查得許多資料,卻都未能完成我所想要的結果,經過一番努力總算成功運用這些api,下面列出幾個關鍵點,在此記錄並分享。

1. APN connect
lResult = ConnMgrMapConRef(ConRefType_NAP,APN_Name, &guidNetwork);

CONNMGR_CONNECTIONINFO ConnInfo;
ConnInfo.dwFlags = CONNMGR_FLAG_PROXY_HTTP | CONNMGR_FLAG_PROXY_WAP | CONNMGR_FLAG_PROXY_SOCKS4 | CONNMGR_FLAG_PROXY_SOCKS5;
ConnInfo.xxx= xxxx... ;
HRESULT hResult = ConnMgrEstablishConnectionSync(&ConnInfo, &phConnection, 10*1000, &dwStatus );
if( FAILED(hResult))
Add_Log(hWnd, _T("Connected failed!! Please retry your connection..."));
else{
Add_Log(hWnd, _T("APN connected!!"));
hResult = ConnMgrConnectionStatus(phConnection,&dwStatus);
if (dwStatus == CONNMGR_STATUS_CONNECTED) {
Add_Log(hWnd, _T("Internet connection succesfully completed."));
}
}

2. Set the internet open type:

hHttpOpen = InternetOpen( L"Test",
INTERNET_OPEN_TYPE_PROXY, // use proxy tag
L"http://xx.xx.xx.xx:5408", // [proxy url]:[port]
0,
0 );

3. Open a HTTP session for a given site:
hHttpSession = InternetConnect( hHttpOpen,
(LPCWSTR)L"mmm.mmm.mmm.mmm", // dest host, 如 www.mobile01.com
5978, // dest port number
NULL,
NULL,
INTERNET_SERVICE_HTTP,
0,
0 );

4. Creates an HTTP request handle:
hHttpRequest = HttpOpenRequest( hHttpSession,
(LPCWSTR)L"GET",
(LPCWSTR)L"/xxx/xxx/xxx.xxx",//target in host, 如 /index.php
HTTP_VERSION,
NULL,
NULL,
INTERNET_FLAG_DONT_CACHE,
0 );


5. Sends the specified request to the HTTP server:
if(!HttpSendRequest( hHttpRequest,NULL, 0, NULL, 0))
{
Add_Log(hWnd, _T(" HttpSendRequest Failed! (%x)\n", GetLastError()));
InternetCloseHandle(hHttpOpen);
InternetCloseHandle(hHttpSession);
InternetCloseHandle(hHttpRequest);
return false;
}

6. Retrieves header information associated with an HTTP request:
HttpQueryInfo ( hHttpRequest,
HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER,
&dwStatus,
&dwLengthSizeBuffer,
NULL );
if (dwStatus == 404)
Add_Log(hWnd,_T(" Connection Failed."));
else if (dwStatus == 200)
Add_Log(hWnd,_T(" Succeed."));
else
Add_Log(hWnd,_T(" Unknown Response."));

wchar_t errorMsg[20]={0};
wsprintf(errorMsg, L"Response:: %u", dwStatus);
Add_Log(hWnd,errorMsg);

以下這個網站可幫我們將source code給highlight出來,挺方便的
http://tohtml.com/cpp/

沒有留言: