Web Brower 模拟浏览器 post方式提交代码

HRESULT GetPostData(LPVARIANT pvPostData)
{
HRESULT hr;
LPSAFEARRAY psa;
LPCTSTR cszPostData = "type=1&system=4&task=100&status=-1&num=1&data=http%3A%2F%2Fsina.com.cn%0A";// w=企业&y=3;donotcheckip=1&keys=卫浴洁具,玫瑰暗香
UINT cElems = lstrlen(cszPostData);
LPSTR pPostData;

if (!pvPostData)
{
return E_POINTER;
}

VariantInit(pvPostData);

psa = SafeArrayCreateVector(VT_UI1, 0, cElems);
if (!psa)
{
return E_OUTOFMEMORY;
}

hr = SafeArrayAccessData(psa, (LPVOID*)&pPostData);
memcpy(pPostData, cszPostData, cElems);
hr = SafeArrayUnaccessData(psa);

V_VT(pvPostData) = VT_ARRAY | VT_UI1;

V_ARRAY(pvPostData) = psa;
return NOERROR;
}


void CGetdeswordDlg::OnOK()
{
UpdateData(TRUE);


// m_StrEdit = "http://192.168.14.249/np/portal/portlets";
if (!m_StrEdit.IsEmpty())
{

VARIANT postData;
HRESULT hr;
hr = GetPostData(&postData);

m_web.Navigate (m_StrEdit, 0, 0, &postData, 0);

// m_web.Navigate (m_StrEdit, 0, 0, 0, 0);
}
}
一样发送并不成功,主要是Navigate 最后一个参数没有设对.需要定义vHeaders


定义方法参看下面的案例

具体参数细节请参看MSDN文档。比较麻烦的是这个函数的参数设置,后面作详细说明,先看代码。这里有如下假定
iBrowser是一个有效的IWebBrowser2指针,
url为有效的地址(如:L"http://172.24.1.241/profile/"),
postData为需要提交的数据(
如:L"userid=kesalin&password=pwd&action=profile")
//飘飘白云(l_zhaohui@163.com)2007/11/8
IWebBrowser2* iBrowser;
LPCTSTR url;
LPCTSTR postData;
// .......other codes.
//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
vNull.vt = VT_BSTR;
vNull.bstrVal = NULL;
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;
SafeArrayDestroy(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);
有三点值得说明:第一, 数据类型 VT_I4 是指示一个long型的数据,所以要设置 VARIANT的lVal作为值。如代码中的:
vFlags.vt = VT_I4;
vFlags.lVal = navNoReadFromCache | navNoWriteToCache;
第二, 如果要实现POST数据提交,不能将Navigate2方法中不需要的参数简单地设置成NULL,必须创建一个VARIANT变量vNull,并对它进行初始化。比如:
VariantInit(&vNull);
第三, vPostData的变量类型是VT_ARRAY | VT_UI1,其数据是一个SAFEARRAY型的字符数组,具体赋值请参看代码。
// Set value
vNull.vt = VT_BSTR;
vNull.bstrVal = NULL;
iBrowser->Navigate2(&vURL, &vFlags, &vNull, &vPostData, &vHeaders);




 

上一篇: 使用InternetSetCookie删除cookie
下一篇: vc删除文件夹

点击获得Trackback地址

发表评论

内 容: