VC中通过Webbrowser提交表单

本文转载CSDN论坛帖子回复,转载请说明出处!!!链接为 http://bbs.csdn.net/topics/360249823!!!
VOID CDlgIEDlg::AutoFillForm(IHTMLDocument2 *pIHTMLDocument2,CComVariant userID,CComVariant password)
{

if( !pIHTMLDocument2 )return; 

HRESULT hr; 
CComBSTR bstrTitle; 
pIHTMLDocument2->get_title( &bstrTitle );//取得文档标题 

CComQIPtr< IHTMLElementCollection > spElementCollection; 
hr = pIHTMLDocument2->get_forms( &spElementCollection );//取得表单集合 
if ( FAILED( hr ) ) 

AfxMessageBox("获取表单的集合 IHTMLElementCollection 错误"); 
return; 

long nFormCount=0;//取得表单数目 
hr = spElementCollection->get_length( &nFormCount ); 
if ( FAILED( hr ) ) 

AfxMessageBox("获取表单数目错误"); 
return; 


for(long i=0; i<nFormCount; i++) 

IDispatch *pDisp = NULL;//取得第 i 项表单 
hr = spElementCollection->item( CComVariant( i ), CComVariant(), &pDisp ); 
if ( FAILED( hr ) )continue; 

CComQIPtr< IHTMLFormElement > spFormElement = pDisp; 
pDisp->Release(); 

long nElemCount=0;//取得表单中 域 的数目 
hr = spFormElement->get_length( &nElemCount ); 
if ( FAILED( hr ) )continue; 

for(long j=0; j<nElemCount; j++) 

CComDispatchDriver spInputElement;//取得第 j 项表单域 
CComVariant vName,vVal,vType;//取得表单域的名,值,类型 
hr = spFormElement->item( CComVariant( j ), CComVariant(), &spInputElement ); 
if ( FAILED( hr ) )continue; 
hr = spInputElement.GetPropertyByName(L"name", &vName); 
if(vName == (CComVariant)"userID")//找到name属性值为useID的标签
{
vVal = userID;
spInputElement.PutPropertyByName(L"value",&vVal);
}
if(vName == (CComVariant)"password")//找到name属性值为password的标签
{
vVal = password;
spInputElement.PutPropertyByName(L"value",&vVal);
spFormElement->submit();

}


}

你可能感兴趣的:(VC,WebBrowser,提交表单)