Get方法的发送及接收数据
void Connect(void)
{
::CoInitialize(NULL); //Initialize COM componet.
int iLen;
BSTR Text=NULL;
BSTR bstrURL;
HRESULT hr;
VARIANT vEmpty;
if(SUCCEEDED(OleInitialize(NULL)))
{
IWebBrowser2* pBrowser2;
CoCreateInstance(CLSID_InternetExplorer,NULL,CLSCTX_LOCAL_SERVER,IID_IWebBrowser2,(void**)&pBrowser2);
if(pBrowser2 != NULL)
{
VariantInit(&vEmpty);
bstrURL = SysAllocString(L"http://www.baidu.com");
hr = pBrowser2->Navigate(bstrURL,&vEmpty,&vEmpty,&vEmpty,&vEmpty);
READYSTATE r;
for(;;)
{
Sleep(600);
HRESULT hr1 = pBrowser2->get_ReadyState(&r);
if (SUCCEEDED(hr1) && r == READYSTATE_COMPLETE)
break;
}
BSTR Text = NULL;
int iLen;
char* buffer;
if(SUCCEEDED(hr))
{
pBrowser2->put_Visible(VARIANT_FALSE);
CComPtr <IDispatch > spDispDoc;
hr = pBrowser2->get_Document(&spDispDoc);
CComQIPtr <IHTMLDocument2> spDocument2 = spDispDoc;
CComPtr < IHTMLElement > dm_pBoy=NULL,pelmBody = NULL;
spDocument2->get_body(&dm_pBoy);
Text=NULL;
dm_pBoy->get_outerText(&Text);
iLen = SysStringLen(Text);
buffer = (char*)malloc(iLen);
ZeroMemory(buffer,iLen);
for(int i=0;i<iLen-1;i++)
sprintf(&(buffer[i]), "%c", Text[i]);
}
}
pBrowser2->Quit();
SysFreeString(bstrURL);
pBrowser2->Release();
OleUninitialize();
}
::CoUninitialize();
}
POST方法的接收数据
{
::CoInitialize(NULL); //Initialize COM componet.
HRESULT hr;
if(SUCCEEDED(OleInitialize(NULL)))
{
IWebBrowser2* pBrowser2;
CoCreateInstance(CLSID_InternetExplorer,NULL,CLSCTX_LOCAL_SERVER,IID_IWebBrowser2,(void**)&pBrowser2);
if(pBrowser2 != NULL)
{
WCHAR postData[256] = {L"This is a Test.\n"};
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;
vFlags.vt = VT_I4;
vFlags.lVal = navNoReadFromCache | navNoWriteToCache;
vURL.vt = VT_BSTR;
vURL.bstrVal = SysAllocString(L"http://www.baidu.com");
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;
hr = pBrowser2->Navigate2(&vURL, &vFlags, &vNull, &vPostData, &vNull);
READYSTATE r;
for(;;)
{
Sleep(600);
HRESULT hr1 = pBrowser2->get_ReadyState(&r);
if (SUCCEEDED(hr1) && r == READYSTATE_COMPLETE)
break;
}
BSTR Text = NULL;
int iLen;
char* buffer;
if(SUCCEEDED(hr))
{
pBrowser2->put_Visible(VARIANT_FALSE);
CComPtr <IDispatch > spDispDoc;
hr = pBrowser2->get_Document(&spDispDoc);
CComQIPtr <IHTMLDocument2> spDocument2 = spDispDoc;
CComPtr < IHTMLElement > dm_pBoy=NULL,pelmBody = NULL;
spDocument2->get_body(&dm_pBoy);
Text=NULL;
dm_pBoy->get_outerText(&Text);
iLen = SysStringLen(Text);
buffer = (char*)malloc(iLen);
ZeroMemory(buffer,iLen);
for(int i=0;i<iLen-1;i++)
sprintf(&(buffer[i]), "%c", Text[i]); //返回数据在buffer中
}
SafeArrayDestroy(sfPost);
delete[] pPostData;
pPostData = NULL;
pChar = NULL;
}
pBrowser2->Quit();
pBrowser2->Release();
OleUninitialize();
}
::CoUninitialize();
}