html

右键打开的源码有些已经是执行过的结果了.

要获得源码有很多方式,除了前面几位说的,还可以用CHtmlView类来实现.
C/C++ code
   
   
void COpnPageDlg::OnBnClickedButton1() { // TODO: 在此添加控件通知处理程序代码 if (m_pHtmlView == NULL) { m_pHtmlView = new CHtmlViewEx(); RECT rect; GetDlgItem(IDC_PAGE_AREA) -> GetWindowRect( & rect); ScreenToClient( & rect); m_pHtmlView -> Create(NULL,NULL,WS_CHILD | WS_VISIBLE,rect, this ,IDC_PAGE_AREA); m_pHtmlView -> ShowWindow(SW_SHOW); } m_pHtmlView -> Navigate(TEXT( " http://passport.51yx.com/register/newAccountRegister.jsp " )); // C:\\Documents and Settings\\cqjzf\\桌面\\index.html http://passport.51yx.com/register/newAccountRegister.jsp // OutputDebugString(GenerationId()); // OutputDebugString(ChinesWord()); } void COpnPageDlg::OnBnClickedButton2() { // TODO: 在此添加控件通知处理程序代码 if (m_pHtmlView) { IHTMLDocument2 * pIHTMLDoc = (IHTMLDocument2 * )m_pHtmlView -> GetHtmlDocument(); // 这里获得了整个网页的内容 if (pIHTMLDoc) { // EnumFrame(pIHTMLDoc); CString Str; CComQIPtr < IHTMLElementCollection > spAllElementCollection; HRESULT hr = pIHTMLDoc -> get_all( & spAllElementCollection ); // long nAllCount = 0 ; hr = spAllElementCollection -> get_length( & nAllCount); Str.Format(TEXT( " all: %d " ),nAllCount); OutputDebugString(Str); for ( long i = 0 ; i < nAllCount ; i ++ ) { IDispatch * pDisp = NULL; // 取得第 i 个Link spAllElementCollection -> item( CComVariant(i) , CComVariant() , & pDisp); if ( pDisp == NULL ) continue ; CComQIPtr < IHTMLElement > spElement = pDisp; pDisp -> Release(); if ( ! spElement) continue ; CComBSTR bstrSrc; spElement -> get_tagName( & bstrSrc); if (_tcsicmp(bstrSrc.m_str,TEXT( " HTML " )) == 0 ) { spElement -> get_outerText( & bstrSrc); OutputDebugString(_tcsstr(bstrSrc.m_str,TEXT( " 用户名不能为空 " ))); /* if(_tcsstr(bstrSrc.m_str,TEXT("用户名不能为空"))!=0) { OutputDebugString(bstrSrc.m_str); } */ } /* CString tStr; tStr+=bstrSrc.m_str; tStr+=" "; spElement->get_outerText(&bstrSrc); tStr+=bstrSrc.m_str; tStr+=" "; OutputDebugString(tStr); if(_tcsicmp(bstrSrc.m_str,TEXT("HTML"))==0) { OutputDebugString(TEXT("click")); spElement->click(); } */ } } } } HRESULT COpnPageDlg::EnumFrame(IHTMLDocument2 * pHtmlDoc) { if (pHtmlDoc == NULL) { return - 1 ; } CComPtr < IHTMLFramesCollection2 > spFramesCollection2; pHtmlDoc -> get_frames( & spFramesCollection2 ); // 取得框架frame的集合 long nFrameCount = 0 ; // 取得子框架个数 HRESULT hr = spFramesCollection2 -> get_length( & nFrameCount ); if (hr >= 0 && nFrameCount > 0 ) { CString Str; Str.Format(TEXT( " frames:%d " ),nFrameCount); OutputDebugString(Str); for ( int i = 0 ;i < nFrameCount;i ++ ) { CComVariant vDispWin2; // 取得子框架的自动化接口 hr = spFramesCollection2 -> item( & CComVariant(i) , & vDispWin2); // 取得子框架的 IHTMLWindow2 接口 CComQIPtr < IHTMLWindow2 > spWin2 = vDispWin2.pdispVal; if ( ! spWin2 ) continue ; CComPtr < IHTMLDocument2 > spDoc2; spWin2 -> get_document( & spDoc2 ); // 取得子框架的 IHTMLDocument2 接口 EnumFrame(spDoc2); CComQIPtr < IHTMLElementCollection > spAllElementCollection; hr = pHtmlDoc -> get_all( & spAllElementCollection ); // long nAllCount = 0 ; hr = spAllElementCollection -> get_length( & nAllCount); Str.Format(TEXT( " all: %d " ),nAllCount); OutputDebugString(Str); for ( long i = 0 ; i < nAllCount ; i ++ ) { IDispatch * pDisp = NULL; // 取得第 i 个Link spAllElementCollection -> item( CComVariant(i) , CComVariant() , & pDisp); if ( pDisp == NULL ) continue ; CComQIPtr < IHTMLElement > spElement = pDisp; pDisp -> Release(); if ( ! spElement) continue ; CComBSTR bstrSrc; spElement -> get_tagName( & bstrSrc); CString tStr; tStr += bstrSrc.m_str; tStr += " " ; if (_tcsicmp(bstrSrc.m_str,TEXT( " A " )) == 0 ) { spElement -> get_outerText( & bstrSrc); tStr += bstrSrc.m_str; tStr += " " ; } OutputDebugString(tStr); } } } return hr; }


 

 

我目前的程序是mfc dialog模式,且无法支持CDHtmlDialog类,估计也不能使用CHtmlView类,怎么连接互联网也成了问题。

 

nimomike回复于17日07点00分 

void CMainDetailView::OnDetailviewSave()
{
CString strMime;
IHTMLDocument2Ptr pHTMLDoc2 = NULL;
IPersistFilePtr lpPersistFile = NULL;
LPDISPATCH lpDispatch = NULL;
HRESULT hr;
BOOL bOk = FALSE;

lpDispatch = GetHtmlDocument();
if(lpDispatch != NULL)
{
hr = lpDispatch->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&pHTMLDoc2);
if(SUCCEEDED(hr))
{
BSTR bstrType;
pHTMLDoc2->get_mimeType(&bstrType);
strMime = bstrType;
::SysFreeString(bstrType);
CString strFilter;
if(m_bIsXml)
strFilter = _T("XML文档(*.xml)|*.xml||");
else
strFilter = _T("HTML网页(*.htm,*.html)|*.htm;*.html||");
CFileDialog dlg(FALSE, _T("*.htm"), NULL,OFN_OVERWRITEPROMPT, strFilter);
if(dlg.DoModal() == IDOK)
{
try
{
if(m_bIsXml)
{
bOk = CopyFile(m_strTempXmlFile, dlg.GetPathName(), FALSE);
}
else
{
CFile file;
if(file.Open(dlg.GetPathName(), CFile::modeCreate | CFile::modeWrite))
{
CString str;
GetSource(str);
str.TrimRight(0xfeee);
str.TrimRight(0xabab);
file.Write(str.GetBuffer(str.GetLength()), str.GetLength() * sizeof(TCHAR));
str.ReleaseBuffer();
bOk = TRUE;
}
}
}
catch(...)
{
}
}
else
bOk = TRUE;
}

lpDispatch->Release();
}

if(!bOk)
MessageBox(_T("保存页面文件失败!"), _T("错误"), MB_ICONERROR);
}

 

void CMainDetailView::OnDetailviewSave()
{
CString strMime;
IHTMLDocument2Ptr pHTMLDoc2 = NULL;
IPersistFilePtr lpPersistFile = NULL;
LPDISPATCH lpDispatch = NULL;
HRESULT hr;
BOOL bOk = FALSE;

lpDispatch = GetHtmlDocument();
if(lpDispatch != NULL)
{
hr = lpDispatch->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&pHTMLDoc2);
if(SUCCEEDED(hr))
{
BSTR bstrType;
pHTMLDoc2->get_mimeType(&bstrType);
strMime = bstrType;
::SysFreeString(bstrType);
CString strFilter;
if(m_bIsXml)
strFilter = _T("XML文档(*.xml)|*.xml||");
else
strFilter = _T("HTML网页(*.htm,*.html)|*.htm;*.html||");
CFileDialog dlg(FALSE, _T("*.htm"), NULL,OFN_OVERWRITEPROMPT, strFilter);
if(dlg.DoModal() == IDOK)
{
try
{
if(m_bIsXml)
{
bOk = CopyFile(m_strTempXmlFile, dlg.GetPathName(), FALSE);
}
else
{
CFile file;
if(file.Open(dlg.GetPathName(), CFile::modeCreate | CFile::modeWrite))
{
CString str;
GetSource(str);
str.TrimRight(0xfeee);
str.TrimRight(0xabab);
file.Write(str.GetBuffer(str.GetLength()), str.GetLength() * sizeof(TCHAR));
str.ReleaseBuffer();
bOk = TRUE;
}
}
}
catch(...)
{
}
}
else
bOk = TRUE;
}

lpDispatch->Release();
}

if(!bOk)
MessageBox(_T("保存页面文件失败!"), _T("错误"), MB_ICONERROR);
}

 

 

#include <afxinet.h> CInternetSession session; CStdioFile *pFile=sesion.OpenURL("www.google.com.cn"); CString strTemp,strHtml; strHtml=""; while(pFile->ReadString(strTmep)) { strTemp+='\n'; strHtml+=strTemp; }

 

 

 

第一步使用InternetConnect函数  
第二步使用InternetOpenUrl函数  
第三步使用InternetReadFile函数  

this article was contributed by asif rasheed.   

wininet is a high-level interface to the more complicated underlying internet protocols (including http, ftp, and gopher).  
wininet allows your application to act as an http, ftp, or gopher client without its having to understand or, more importantly, keep up with the ever-evolving protocol standards. if you use wininet in your applications, when standards change you can let wininet worry about the changes while your interface to the protocol remains the same. wininet can be used to write product-ordering systems, stock tickers/analyzers, online banking systems, ftp clients, your own internet browser, and so on. before wininet, adding internet communications to windows-based applications required expertise in sockets and protocol specifications. even simple communications required considerable development time. wininet lets you quickly and easily add internet communications to your applications.  

mfc also implemented some class which uses these apis. these classes are distributed in different hierarchies. i develop a small class for that which has only two methods. by introducing this class in project and calling one method, one can easily download the web page from given url.  

this class has two methods,  

  cstring getwebpage(const cstring& url);  
  void seterrormessage(cstring s);  


getwebpage method is used for accepting the url (it must me complete i.e., http:\\www.codeguru.com) and returning the desired page.  

seterrormessage method receives the default error message. when there was some error due to any reason, getwebpage method will return this message. i am working on it and in future beside default error message, a actual error message will be also transmitted.  


/*  
//------------------------------------------------------------------------------------------------------------------  
// webworld.h: interface for the cwebworld class.  
//------------------------------------------------------------------------------------------------------------------  
*/  

#include "wininet.h"  

class cwebworld   
{  
public:  
  void seterrormessage(cstring s);  
  cstring getwebpage(const cstring& url);  
  cwebworld();  
  virtual ~cwebworld();  

private:  
  cstring m_errormessage;  
  hinternet m_session;  
};  


/*  
//------------------------------------------------------------------------------------------------------------------  
// webworld.cpp: implementation of the cwebworld class.  
//------------------------------------------------------------------------------------------------------------------  
*/  

#include "stdafx.h"  
#include "webthief.h"  


#ifdef _debug  
#undef this_file  
static char this_file[]=__file__;  
#define new debug_new  
#endif  

#define agent_name "codegurubrowser1.0"  

//  
// construction/destruction  
//  

cwebworld::cwebworld()  
{  
  dword dwerror;  

  // initialize the win32 internet functions   
  m_session = ::internetopen(agent_name,   
  internet_open_type_preconfig, // use registry settings.   
  null, // proxy name. null indicates use default.  
  null, // list of local servers. null indicates default.   
  0) ;  

  dwerror = getlasterror();  
}  

cwebworld::~cwebworld()  
{  
  // closing the session  
  ::internetclosehandle(m_session);  
}  

cstring cwebworld::getwebpage(const cstring& url)  
{  
  hinternet hhttpfile;  
  char szsizebuffer[32];  
  dword dwlengthsizebuffer = sizeof(szsizebuffer);   
  dword dwfilesize;  
  dword dwbytesread;  
  bool bsuccessful;  
  cstring contents;  

  // setting default error message  
  contents = m_errormessage;  
    
  // opening the url and getting a handle for http file  
  hhttpfile = internetopenurl(m_session, (const char *) url, null, 0, 0, 0);  

  if (hhttpfile)  
  {   
  // getting the size of http files  
  bool bquery = ::httpqueryinfo(hhttpfile,http_query_content_length, szsizebuffer, &dwlengthsizebuffer, null) ;  

  if(bquery==true)  
  {   
  // allocating the memory space for http file contents  
  dwfilesize=atol(szsizebuffer);  
  lpstr szcontents = contents.getbuffer(dwfilesize);  

  // read the http file   
  bool bread = ::internetreadfile(hhttpfile, szcontents, dwfilesize, &dwbytesread);   
    
  if (bread)   
  bsuccessful = true;  

  ::internetclosehandle(hhttpfile); // close the connection.  
  }  

  }  
  else  
  {  
  // connection failed.  
  bsuccessful = false;  
  }   
  return contents;  
}  

void cwebworld::seterrormessage(cstring s)  
{  
  m_errormessage = s;  
}  


following is a use of above class.  

  cwebworld a;   
  cstring pagecontent;  

  a.seterrormessage("there is some error in getting web page ... ");  
  pagecontent = a.getwebpage(m_url);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值