请教一下各位达人,在下刚开始使用webbrowser,现在想制作一个程序,自动向某个网页POST一个信息,并且点击它页面上的一个按钮,比如百度的首页;请问该如何做呢?我在CSDN上查了不少资料,基本都是VB的,少数几个VC的也说的语焉不详,让人摸不着头脑。比如我要在百度上输入123,然后点击搜索,我自己这样写的代码,可以正确打开网页,但是却无法成功POST信息;并且,我更不知道如何点击按钮了…… 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);
那个搜索按钮的代码是“<input type="submit" value="百度一下" id="su" class="btn"”请各位达人麻烦指点一下,谢谢,网上相关资料不少,但对应的真的很少,更找不到示范的代码……

解决方案 »

  1.   


    …………老大,我必须用webbrowser,还要点击给人看的
      

  2.   

    并且,最开始我也用ChttpFile试过,可是自己写的代码不对,没办法投递: 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;
    }
    返回的仍然是百度的首页而不是搜索结果页面
      

  3.   

    要么你用IHtmlDocument2的ExecScript来执行一段Ajax来POST
      

  4.   

    这里打字太快写错了,应该是IHTMLWindow2::execScript,而不是IHtmlDocument2
      

  5.   

    我只知道webbrowser组件……还是刚了解到的。
      

  6.   

    那我觉得你还是用WinInet吧,好理解些
      

  7.   

    找到代码,学习了下,搞定了。
    用webbrowser完成的,既然没人贴代码帮忙,就分享出来吧。
    //安装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();
    }