自动登录网页

 

//版本1

#include <mshtml.h>   
#include <atlbase.h>
#include <oleacc.h>

 

.h文件

 

CComQIPtr<IWebBrowser2> pBrowser;

 BOOL bReady;       //判断yahoo邮箱页面是否完全打开
 CString m_strURL;
 CString m_strUserName;
 CString m_strPassWord;

.cpp文件

 

m_strURL = "http://cn.mail.yahoo.com/";  //URL地址
 m_strUserName = "name";      //邮箱用户名
 m_strPassWord = "password";     //用户密码
 
 VARIANT id, index;
 
 BSTR bsStatus;
 bReady=0;
 BSTR bsPW = m_strPassWord.AllocSysString();
 BSTR bsUser=m_strUserName.AllocSysString();
 
 CString mStr;
 HRESULT hr;
 //创建COM组件
 hr = CoInitialize(NULL);
 if(!SUCCEEDED(hr))
 {
  return;
 }
 
 hr = CoCreateInstance (CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER,
  IID_IWebBrowser2, (LPVOID *)&pBrowser); //Create an Instance of web browser
 if(hr!=S_OK)
 {
  char a[1024] = {0};
  DWORD b = GetLastError();
  sprintf(a,"CoCreateInstance fail a=%d hr = 0x%x",b,hr);
  return;
 }
 
 VARIANT_BOOL pBool=true;
 //显示出浏览器
 pBrowser->put_Visible( pBool ) ; //Commentout this line if you dont want the browser to be displayed
 COleVariant vaURL(m_strURL) ;
 COleVariant null;
 //浏览指定网址
 pBrowser->Navigate2(vaURL,null,null,null,null) ; //Open the URL page


 READYSTATE emReadState;
 //等待网页打开
 pBrowser->get_ReadyState(&emReadState);
 while (READYSTATE_COMPLETE != emReadState)
 {
  pBrowser->get_ReadyState(&emReadState);
  Sleep(500);
 }
//  while(!bReady)  //This while loop maks sure that the page is fully loaded before we go to the next page
//  {
//   //如果用户手动关闭IE窗口,退出循环
//   LONG hHwnd;
//   pBrowser->get_HWND(&hHwnd);
//   if (NULL == hHwnd)
//   {
//    bReady=1;
//    return;
//   }
//   
//   //等待网页完全打开,退出循环
//   pBrowser->get_StatusText(&bsStatus);
//   mStr=bsStatus;
//   if(mStr=="完毕" || mStr=="完成" || mStr=="Done" )
//   {
//    bReady=1;
//   }
//   
//   Sleep(200);
//  }
 
 CComPtr<IDispatch> pDisp;//IDispatch* pDisp;
 hr=pBrowser->get_Document(&pDisp);//Get the underlying document object of the browser
 
 if (pDisp == NULL )
 {
  CoUninitialize();
  return;
 }
 
 CComPtr<IHTMLDocument2> pHTMLDocument2;
 //获得HTMLDocument2接口
 hr = pDisp->QueryInterface( IID_IHTMLDocument2,
  (void**)&pHTMLDocument2 );//Ask for an HTMLDocument2 interface
 
 if (hr != S_OK)
 {
  CoUninitialize();
  return ;
 }
 
 CComPtr<IHTMLElementCollection> pColl = NULL;//Enumerate the HTML elements
 //获得元素集合ElementCollection接口
 hr = pHTMLDocument2->get_all( &pColl );
 if (hr != S_OK || pColl == NULL)
 {
  pHTMLDocument2.Release();
  CoUninitialize();
  return;
 }
 //读取元素集合长度
 LONG celem;
 hr = pColl->get_length( &celem );//Find the count of the elements
 
 if ( hr != S_OK )
 {
  pHTMLDocument2.Release();
  pColl.Release();
  CoUninitialize();
  return;
 }
 //遍历元素
 for ( int i=0; i< celem; i++ )//Loop through each elment
 {
  CComPtr<IDispatch> pDisp2;
  
  V_VT(&id) = VT_I4;
  V_I4(&id) = i;
  V_VT(&index) = VT_I4;
  V_I4(&index) = 0;
  //获得元素Dispitem
  hr = pColl->item( id,index, &pDisp2 );//Get an element
  
  if ( hr == S_OK )
  {
   IHTMLElement* pElem;
   //获得常规网页元素HTMLElement接口
   //Ask for an HTMLElemnt interface
   hr = pDisp2->QueryInterface(IID_IHTMLElement,(void **)&pElem);
   if ( hr == S_OK )
   {
    BSTR bstr;
    //试图获得输入文本框元素接口
    IHTMLInputTextElement* pUser;//We need to check for input elemnt on login screen
    hr = pDisp2->QueryInterface(IID_IHTMLInputTextElement,(void **)&pUser );
    if ( hr == S_OK )
    {
     pUser->get_name(&bstr);
     mStr=bstr;
     if(mStr=="login")
     {//Is this a User Id frield
      pUser->put_value(bsUser);//Paste the User Id
     }
     else if(mStr=="passwd")
     {//Or, is this a password field
      pUser->put_value(bsPW);// Paste your password into the field                
     }
     pUser->Release();
     
    }
    else
    {
     IHTMLInputButtonElement* pButton;
     hr = pDisp2->QueryInterface(
      IID_IHTMLInputButtonElement,
      (void **)&pButton);
     if ( hr == S_OK )
     {
      //pButton->get_type(&bstr);//This will send the all the information in correct format        
      pButton->get_value(&bstr);
      mStr=bstr;
      if (mStr=="登 录")
      {
       pElem->click();
       
       i=celem;
      }
      pButton->Release();
     }
    }
    pElem->Release();
   }
   pDisp2.Release();
  }
 }
 
 pColl.Release();
 
 pHTMLDocument2.Release();//For the next page open a fresh document
 
 pDisp.Release();
 
 CoUninitialize();

 

 

 

 

//版本2

#include <mshtml.h>   
#include <atlbase.h>
#include <oleacc.h>

 

.h文件

 

IWebBrowser2 * pBrowser;
 BOOL bReady;       //判断yahoo邮箱页面是否完全打开
 CString m_strURL;
 CString m_strUserName;
 CString m_strPassWord;

.cpp文件

 

m_strURL = "http://cn.mail.yahoo.com/";  //URL地址
 m_strUserName = "name";      //邮箱用户名
 m_strPassWord = "password";     //用户密码
 
 VARIANT id, index;
 
 BSTR bsStatus;
 bReady=0;
 BSTR bsPW = m_strPassWord.AllocSysString();
 BSTR bsUser=m_strUserName.AllocSysString();
 
 CString mStr;
 HRESULT hr;
 //创建COM组件
 hr = CoInitialize(NULL);
 if(!SUCCEEDED(hr))
 {
  return;
 }
 
 hr = CoCreateInstance (CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER,
  IID_IWebBrowser2, (LPVOID *)&pBrowser); //Create an Instance of web browser
 if(hr!=S_OK)
 {
  char a[1024] = {0};
  DWORD b = GetLastError();
  sprintf(a,"CoCreateInstance fail a=%d hr = 0x%x",b,hr);
  return;
 }
 
 VARIANT_BOOL pBool=true;
 //显示出浏览器
 pBrowser->put_Visible( pBool ) ; //Commentout this line if you dont want the browser to be displayed
 COleVariant vaURL(m_strURL) ;
 COleVariant null;
 //浏览指定网址
 pBrowser->Navigate2(vaURL,null,null,null,null) ; //Open the URL page


 READYSTATE emReadState;
 //等待网页打开
 pBrowser->get_ReadyState(&emReadState);
 while (READYSTATE_COMPLETE != emReadState)
 {
  pBrowser->get_ReadyState(&emReadState);
  Sleep(500);
 }
//  while(!bReady)  //This while loop maks sure that the page is fully loaded before we go to the next page
//  {
//   //如果用户手动关闭IE窗口,退出循环
//   LONG hHwnd;
//   pBrowser->get_HWND(&hHwnd);
//   if (NULL == hHwnd)
//   {
//    bReady=1;
//    return;
//   }
//   
//   //等待网页完全打开,退出循环
//   pBrowser->get_StatusText(&bsStatus);
//   mStr=bsStatus;
//   if(mStr=="完毕" || mStr=="完成" || mStr=="Done" )
//   {
//    bReady=1;
//   }
//   
//   Sleep(200);
//  }
 
 IDispatch* pDisp;
 hr=pBrowser->get_Document(&pDisp);//Get the underlying document object of the browser
 
 if (pDisp == NULL )
 {
  CoUninitialize();
  return;
 }
 
 IHTMLDocument2* pHTMLDocument2;
 //获得HTMLDocument2接口
 hr = pDisp->QueryInterface( IID_IHTMLDocument2,
  (void**)&pHTMLDocument2 );//Ask for an HTMLDocument2 interface
 
 if (hr != S_OK)
 {
  CoUninitialize();
  return ;
 }
 
 IHTMLElementCollection* pColl = NULL;//Enumerate the HTML elements
 //获得元素集合ElementCollection接口
 hr = pHTMLDocument2->get_all( &pColl );
 if (hr != S_OK || pColl == NULL)
 {
  pHTMLDocument2->Release();
  CoUninitialize();
  return;
 }
 //读取元素集合长度
 LONG celem;
 hr = pColl->get_length( &celem );//Find the count of the elements
 
 if ( hr != S_OK )
 {
  pHTMLDocument2->Release();
  pColl->Release();
  CoUninitialize();
  return;
 }
 //遍历元素
 for ( int i=0; i< celem; i++ )//Loop through each elment
 {
  IDispatch* pDisp2;
  
  V_VT(&id) = VT_I4;
  V_I4(&id) = i;
  V_VT(&index) = VT_I4;
  V_I4(&index) = 0;
  //获得元素Dispitem
  hr = pColl->item( id,index, &pDisp2 );//Get an element
  
  if ( hr == S_OK )
  {
   IHTMLElement* pElem;
   //获得常规网页元素HTMLElement接口
   //Ask for an HTMLElemnt interface
   hr = pDisp2->QueryInterface(IID_IHTMLElement,(void **)&pElem);
   if ( hr == S_OK )
   {
    BSTR bstr;
    //试图获得输入文本框元素接口
    IHTMLInputTextElement* pUser;//We need to check for input elemnt on login screen
    hr = pDisp2->QueryInterface(IID_IHTMLInputTextElement,(void **)&pUser );
    if ( hr == S_OK )
    {
     pUser->get_name(&bstr);
     mStr=bstr;
     if(mStr=="login")
     {//Is this a User Id frield
      pUser->put_value(bsUser);//Paste the User Id
     }
     else if(mStr=="passwd")
     {//Or, is this a password field
      pUser->put_value(bsPW);// Paste your password into the field                
     }
     pUser->Release();
     
    }
    else
    {
     IHTMLInputButtonElement* pButton;
     hr = pDisp2->QueryInterface(
      IID_IHTMLInputButtonElement,
      (void **)&pButton);
     if ( hr == S_OK )
     {
      //pButton->get_type(&bstr);//This will send the all the information in correct format        
      pButton->get_value(&bstr);
      mStr=bstr;
      if (mStr=="登 录")
      {
       pElem->click();
       
       i=celem;
      }
      pButton->Release();
     }
    }
    pElem->Release();
   }
   pDisp2->Release();
  }
 }
 
 pColl->Release();
 
 pHTMLDocument2->Release();//For the next page open a fresh document
 
 pDisp->Release();
 
 CoUninitialize();

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值