3,055
社区成员
发帖
与我相关
我的任务
分享
CString szHeader = "Content-Type: application/x-www-form-urlencoded\r\n";
VARIANT vaHeader = COleVariant(szHeader, VT_BSTR);
CString szPost = "123";
VARIANT vaPost = COleVariant(szPost, VT_BSTR);
CString szURL = "http://www.baidu.com";
m_Web.Navigate(szURL.GetBuffer(), NULL, NULL, &vaPost, &vaHeader);
//安装COM组件webbrowser的方法:
//打开资源视图,在主界面上点击右键选择插入activex控件,弹出的office之类取消,然后会弹出一个
//选择列表,选择microsoft web browser。
//安装完成后,工程中会多出explorer1.h和explorer1.cpp文件,整个项目以unicode为好,有的函数貌似
//仅支持unicode。
CExplorer1 m_Web;
DECLARE_EVENTSINK_MAP()
BEGIN_EVENTSINK_MAP(CtestDlg, CDialog)
ON_EVENT(CtestDlg, IDC_EXPLORER1, 259, CtestDlg::DocumentCompleteExplorer, VTS_DISPATCH VTS_PVARIANT)
END_EVENTSINK_MAP()
void CtestDlg::DocumentCompleteExplorer(LPDISPATCH pDisp, VARIANT* URL)
{
}
void CtestDlg::OnBnClickedButtonStart()
{
m_Web.Navigate(L"http://www.baidu.com", NULL, NULL, NULL, NULL);
CString szText;
Edit_Input.GetWindowText(szText);
if (true == szText.IsEmpty())
{
return;
}
CoInitialize(NULL);
CComQIPtr<IHTMLDocument2> spDoc = m_Web.get_Document();
if (NULL == spDoc)
{
return;
}
HRESULT hRes;
long nFormCount = 0; //表单数目
CComBSTR bstrTitle;
CComQIPtr<IHTMLElementCollection> spElementCollection;
CComQIPtr<IHTMLElement> spIHTMLEle;
//取得文档标题
spDoc->get_title(&bstrTitle);
//取得表单集合
hRes = spDoc->get_forms(&spElementCollection);
if (FAILED(hRes))
{
return;
}
hRes = spDoc->get_body(&spIHTMLEle);
if (FAILED(hRes))
{
return;
}
hRes = spElementCollection->get_length(&nFormCount);
if (FAILED(hRes))
{
return;
}
for (long i = 0; i < nFormCount; ++ i)
{
IDispatch *pDisp = NULL;
//取得第 i 项表单
hRes = spElementCollection->item(CComVariant(i), CComVariant(), &pDisp);
if (FAILED(hRes))
{
continue;
}
CComQIPtr<IHTMLFormElement> spFormElement = pDisp;
pDisp->Release();
long nElemCount = 0; //表单中域的数目
hRes = spFormElement->get_length(&nElemCount);
if (FAILED(hRes))
{
continue;
}
USES_CONVERSION;
for (long j = 0; j < nElemCount; ++ j)
{
CComDispatchDriver spInputElement;
//取得第 j 项表单域
hRes = spFormElement->item(CComVariant( j ), CComVariant(), &spInputElement);
if (FAILED(hRes))
{
continue;
}
CComVariant vID;
CComVariant vType;
hRes = spInputElement.GetPropertyByName(L"id", &vID);
if (FAILED(hRes))
{
continue;
}
hRes = spInputElement.GetPropertyByName(L"type", &vType);
if (FAILED(hRes))
{
continue;
}
LPCTSTR lpVID = vID.bstrVal ? OLE2CT(vID.bstrVal) : _T("NULL");
LPCTSTR lpType = vType.bstrVal ? OLE2CT(vType.bstrVal) : _T("NULL");
CString strID(lpVID);
CString strType(lpType);
//kw是百度首页文本框ID
if (0 == strID.CompareNoCase(_T("kw")) && 0 == strType.CompareNoCase(_T("text")))
{
CComVariant vValue = szText;
spInputElement.PutPropertyByName(L"value", &vValue);
}
//这里并没有点击搜索按钮,但执行后还是可以得到搜索结果页面,从这里可以看出一点网页
//数据的执行机制;理论上,对百度页面POST "/s?wd=11111"消息就意味着获取输入框值为
//11111的搜索结果(当然百度实际上还有其它的措施)。
//如果不执行submit()这一句,就可以看到文本已被投递到文本框上,但尚未执行。
spFormElement->submit();
}
pDisp->Release();
}
CoUninitialize();
}
CString szHeader = "Content-Type: application/x-www-form-urlencoded\r\n";
CString szData = "123";
CString szURL = "http://www.baidu.com";
CString strSentence, strGetSentence;
CInternetSession Session;
CHttpFile *lpHttpFile = NULL;
try
{
lpHttpFile = (CHttpFile*)Session.OpenURL(szURL);
if (NULL == lpHttpFile)
{
return;
}
DWORD dwStatus;
DWORD dwBuffLen = sizeof(dwStatus);
BOOL bRes = lpHttpFile->QueryInfo(HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwBuffLen);
if (FALSE == bRes)
{
return;
}
if (dwStatus < 200 || dwStatus > 300)
{
return;
}
bRes = lpHttpFile->SendRequest(szHeader, (LPVOID)(LPCTSTR)szData, szData.GetLength());
if (FALSE == bRes)
{
return;
}
while(lpHttpFile->ReadString(strSentence))
{
strGetSentence += strSentence + char(13) + char(10);
}
Log(strGetSentence.GetBuffer());
lpHttpFile->Close();
delete lpHttpFile;
}
catch (CException *Error)
{
char szError[1024] = {0};
Error->GetErrorMessage(szError, 512);
throw;
}