開發(fā)中的社區(qū)游戲,要求將原來的mini瀏覽器擴(kuò)展成可以通過Http POST傳輸來打開網(wǎng)頁,比如:通過提交用戶id,password,用戶的action(查看profile,查看日記,打開blog等等),從而在mini瀏覽器里打開相應(yīng)的網(wǎng)頁。
具體實(shí)現(xiàn)是通過獲取瀏覽器的IWebBrowser2指針,用該接口的Navigate2方法來實(shí)現(xiàn),Navigate2在MSDN描述如下,
HRESULT Navigate2(
VARIANT *URL,
VARIANT *Flags,
VARIANT *TargetFrameName,
VARIANT *PostData,
VARIANT *Headers
);
具體參數(shù)細(xì)節(jié)請(qǐng)參看MSDN文檔。比較麻煩的是這個(gè)函數(shù)的參數(shù)設(shè)置,后面作詳細(xì)說明,先看代碼。這里有如下假定
iBrowser是一個(gè)有效的IWebBrowser2指針,
url為有效的地址(如:L"http://172.24.1.241/profile/"),
postData為需要提交的數(shù)據(jù)(
如:L"userid=kesalin&password=pwd&action=profile")
//飄飄白云(l_zhaohui@163.com) 2007/11/8
IWebBrowser2* iBrowser;
LPCTSTR url;
LPCTSTR postData
........//其他代碼
//Get size of Post Data
int size = WideCharToMultiByte(CP_ACP, 0, postData, -1, 0, 0, 0, 0);
VARIANT vURL;
VARIANT vFlags;
VARIANT vPostData;
VARIANT vHeaders;
VARIANT vNull;
// Init
VariantInit(&vURL);
VariantInit(&vFlags);
VariantInit(&vPostData);
VariantInit(&vHeaders);
VariantInit(&vNull);
// Set value
vHeaders.vt = VT_BSTR;
vHeaders.bstrVal = SysAllocString(L"Content-Type: application/x-www-form-urlencoded\r\n");
vFlags.vt = VT_I4;
vFlags.lVal = navNoReadFromCache | navNoWriteToCache;
vURL.vt = VT_BSTR;
vURL.bstrVal = SysAllocString(url);
if (size > 1) {
// POST
char* pPostData = new char[size + 1];
WideCharToMultiByte(CP_ACP, 0, postData, -1, pPostData, size, 0, 0);
SAFEARRAY FAR* sfPost = NULL;
SAFEARRAYBOUND bound;
bound.cElements = (ULONG) (strlen(pPostData));
bound.lLbound = 0;
sfPost = SafeArrayCreate(VT_UI1, 1, &bound);
char* pChar = pPostData;
for (long lIndex = 0; lIndex < (signed)bound.cElements; lIndex++) {
SafeArrayPutElement(sfPost, &lIndex, (void*)((pChar++)));
}
vPostData.vt = VT_ARRAY | VT_UI1;
vPostData.parray = sfPost;
delete[] pPostData;
pPostData = NULL;
pChar = NULL;
iBrowser->Navigate2(&vURL, &vFlags, &vNull, &vPostData, &vHeaders);
}
else {
// GET
iBrowser->Navigate2(&vURL, &vNull, &vNull, &vNull, &vNull);
}
// Clear
VariantClear(&vURL);
VariantClear(&vFlags);
VariantClear(&vPostData);
VariantClear(&vHeaders);
VariantClear(&vNull);
有三點(diǎn)值得說明:
第一, 數(shù)據(jù)類型 VT_I4 是指示一個(gè)long型的數(shù)據(jù),所以要設(shè)置 VARIANT的lVal作為值。如代碼中的:
vFlags.vt = VT_I4;
vFlags.lVal = navNoReadFromCache | navNoWriteToCache;
第二, 如果要實(shí)現(xiàn)POST數(shù)據(jù)提交,不能將Navigate2方法中不需要的參數(shù)簡(jiǎn)單地設(shè)置成NULL,必須創(chuàng)建一個(gè)VARIANT變量vNull,并對(duì)它進(jìn)行初始化。比如:
VariantInit(&vNull);
vNull.vt = VT_BSTR;
vNull.bstrVal = NULL;
……
iBrowser->Navigate2(&vURL, &vFlags, &vNull, &vPostData, &vHeaders);
第三, vPostData的變量類型是VT_ARRAY | VT_UI1,其數(shù)據(jù)是一個(gè)SAFEARRAY型的字符數(shù)組,具體賦值請(qǐng)參看代碼。
聯(lián)系客服