使用纯c语言使用IWebBrowser2发送POST请求



我需要在纯C中使用IWebBrowser2编译一个POST请求例程,问题是由PostData指定的POST数据作为SAFEARRAY数据类型结构传递。变体的类型应该是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"%lsnn", 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);
}
我是这样解决的:
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);
/* Set the POST data */
VARIANT PostData;
PostData.vt = VT_NULL;
CHAR PostDataContent[] = "sub=gt&name=sdfs";
UINT32 PostDatalenght = sizeof(PostDataContent);
// Create safe array
SAFEARRAY* psa = SafeArrayCreateVector(VT_UI1, 0, PostDatalenght);
if (psa != NULL)
{
// Store elements into the SafeArray
BYTE HUGEP* pByte = (PBYTE)&PostDataContent;
HRESULT hr = S_OK;
for (long i=0; i<PostDatalenght; i++){
hr = SafeArrayPutElement(psa, &i, pByte);
pByte++;
}
// Get a pointer to the elements of the array.
SafeArrayAccessData(psa, (void HUGEP**)&pByte);
PostData.vt = VT_ARRAY|VT_UI1;
PostData.parray = psa;
}

/* Set the headers data */
VARIANT Headers;
V_VT(&Headers) = VT_BSTR;
V_BSTR(&Headers) = SysAllocString(L"Content-Type: application/x-www-form-urlencoded");
/* 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"%lsnn", 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);
}

相关内容

  • 没有找到相关文章

最新更新