我正在使用 IWebbrowser2
接口(interface)来调用页面并向其提交一些 POST 数据。 IE 已打开,页面已调用,但不幸的是没有提交 POST 数据(PHP 服务器上的 $_POST 数组为空)。谁能告诉我为什么?
main方法中的相关部分:
BSTR bstrURL2;
bstrURL2 = SysAllocString(L"http://www.mypage.com/");
// Specify a binary Content-Type.
V_BSTR(&vHeaders) = SysAllocString(
L"Content-Type: application/x-www-form-urlencoded\r\n"
L"Content-Encoding: UTF-8\r\n");
VARIANT vPostData = {0};
if (Navigate::GetPostData(&vPostData) == NOERROR)
r->Navigate(bstrURL2, &vFlags, &vEmpty, &vPostData, &vHeaders);
GetPostData 方法:
HRESULT Navigate::GetPostData(LPVARIANT pvPostData)
{
HRESULT hr;
LPSAFEARRAY psa;
LPCTSTR cszPostData = "username=myusername&password=mypassword";
UINT cElems;
LPSTR pPostData;
cElems = lstrlen(cszPostData);
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;
}
最佳答案
我尝试重现您的示例,当我定义 vHeader
变量的类型时它起作用了:
// Specify a binary Content-Type.
V_VT(&vHeaders) = VT_BSTR;
V_BSTR(&vHeaders) = SysAllocString(L"Content-Type: application/x-www-form-urlencoded\r\nContent-Encoding: UTF-8\r\n");
关于c++ - Iwebbrowser2 导航 : No post-data submitted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27775825/