我需要使用IWebBrowser 2在纯C中编译POST请求例程,问题是“PostData指定的post数据作为SAFEARRAY数据类型结构传递。VARIANT应该是VT_ARRAY类型|VT_UI1并指向SAFEARRAY数据类型。SAFEARRAY数据类型应该是元素类型VT_UI1,维度为1,并且元素计数等于post数据的字节数。”如https://learn.microsoft.com/en-us/previous-versions/aa752133(v=vs.85)中所述
当我发送请求到我的本地服务器时,它只是没有接收到$_POST ['name']或$_POST ['sub']变量,就像我什么都没发送一样
INT32
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT32 nShowCmd)
{
HRESULT HResult = 0;
CLSID CLSID_IE;
IWebBrowser2 *pWebBrowser;
VARIANT vEmpty;
VARIANT_BOOL vBusy;
VariantInit(&vEmpty);
CoInitializeEx(0, COINIT_MULTITHREADED);
CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
CoCreateInstance(&CLSID_InternetExplorer, 0, CLSCTX_ALL, &IID_IWebBrowser2, &pWebBrowser);
IWebBrowser2_put_Visible(pWebBrowser, VARIANT_FALSE);
/* process the formulary data */
LPSTR pPostData = NULL;
CHAR strFormulary[] = "sub=gt&name=sdfs";
LPSAFEARRAY FormularyArray = SafeArrayCreateVector(VT_UI1, 0, sizeof(strFormulary));
VARIANT PostData = FormularyArray;
SafeArrayAccessData(FormularyArray, (LPVOID*)&pPostData);
memcpy(pPostData, &strFormulary, sizeof(strFormulary));
SafeArrayUnaccessData(FormularyArray);
/* Set the headers data */
VARIANT Headers;
V_VT(&Headers) = VT_BSTR;
V_BSTR(&Headers) = SysAllocString(L"Content-Type: application/x-www-form-urlencodedrn");
/* Set POST request */
BSTR bstrURL = SysAllocString(L"http://192.168.100.44/user.php");
IWebBrowser2_Navigate(pWebBrowser, bstrURL, &vEmpty, &vEmpty, &PostData, &Headers);
// Wait for page to load
do {
LARGE_INTEGER TimeOut;
UINT32 Milliseconds = 2000;
TimeOut.QuadPart = UInt32x32To64( Milliseconds, 1000 );
TimeOut.QuadPart *= -1;
NtDelayExecution(FALSE, &TimeOut);
IWebBrowser2_get_Busy(pWebBrowser, &vBusy);
} while(vBusy);
// Get IDispatch interface
IDispatch* pDispatch;
IWebBrowser2_get_Document(pWebBrowser, &pDispatch);
IHTMLDocument2* pDocument;
IDispatch_QueryInterface(pDispatch, &IID_IHTMLDocument2 , &pDocument);
IHTMLElement* lpBodyElm;
IHTMLDocument2_get_body(pDocument, &lpBodyElm);
IHTMLElement* lpParentElm;
IHTMLElement_get_parentElement(lpBodyElm, &lpParentElm);
// Get Inner HTML content content of the request
BSTR bstrBody;
lpParentElm->lpVtbl->get_innerHTML(lpParentElm, &bstrBody);
wprintf(L"%ls\n\n", bstrBody);
cleanup:
SysFreeString(bstrURL);
IWebBrowser2_Quit(pWebBrowser);
IWebBrowser2_Release(pWebBrowser);
IDispatch_Release(pDispatch);
IHTMLDocument2_Release(pDocument);
IHTMLElement_Release(lpBodyElm);
lpParentElm->lpVtbl->Release(lpParentElm);
CoUninitialize();
RtlExitUserProcess(STATUS_SUCCESS);
}
1条答案
按热度按时间lp0sw83n1#
我是这么解决的: