Click here to Skip to main content
Click here to Skip to main content

Retrieving the HTML source code

, 30 Nov 2004
Rate this:
Please Sign up or sign in to vote.
An article on how to retrieve the full source code of a web page.


An app I was writing needed to store the full HTML of a web page. I looked all over the web and the MSDN library on how to get the complete HTML from a CHtmlView. I found out how to get the <BODY></BODY> data, but not how to get the <HTML></HTML> data. After lots of stumbling, I hit on the following very simple technique.

Examples of getting the outer HTML of the <BODY> tag abound. While exploring the IHTMLDocument2 interface, I noticed the get_ParentElement method. I realized that the parent of <BODY> is <HTML>.

This function took care of my problem:

bool CMyHtmlView::GetDocumentHTML(CString &str)
    IHTMLDocument2 *lpHtmlDocument = NULL;
    LPDISPATCH lpDispatch = NULL;

    lpDispatch = GetHtmlDocument();
        return false;

    lpDispatch->QueryInterface(IID_IHTMLDocument2, (void**)&lpHtmlDocument);

    IHTMLElement *lpBodyElm;
    IHTMLElement *lpParentElm;

    // get_body returns all between <BODY> and </BODY>. 
    // I need all between <HTML> and </HTML>.

    // the parent of BODY is HTML
    BSTR    bstr;
    str = bstr;


    return true;

Points of Interest

There is bound to be a better way of doing this. If you know it, please share it with me.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Geno Carman

United States United States
No Biography provided

Comments and Discussions

GeneralMy vote of 1 Pinmembersunnic1234567884-Nov-13 16:47 
GeneralAnother way Pinmembersohacc7-Feb-13 4:00 
QuestionDisplay content after body Pinmemberuttamnadiyapara4-Sep-12 21:48 
AnswerRe: Display content after body PinmemberGeno Carman5-Sep-12 19:11 
GeneralRe: Display content after body Pinmemberuttamnadiyapara5-Sep-12 23:33 
Questionerror C2065: 'GetHtmlDocument' : undeclared identifier Pinmemberuttamnadiyapara4-Sep-12 4:12 
AnswerRe: error C2065: 'GetHtmlDocument' : undeclared identifier PinmemberGeno Carman4-Sep-12 14:05 
AnswerExcellent solution Pinmembermrbll5-Jun-12 9:31 
GeneralThanks you! PinmemberStoney Tian26-Nov-10 22:56 
GeneralYou saved my day too ! Pinmemberjohan.julien24-Feb-09 22:57 
GeneralMy Way of Doing It in C# PinmemberSniper16718-Jul-07 20:28 
GeneralAnother way Pinmembervinhie473-Jul-07 17:15 
Questionhow can i get a http-header Pinmemberemmi7-Feb-07 0:51 
QuestionExplaination of the code PinmemberGlen_CodeProj28-Mar-06 19:40 
GeneralRe: Explaination of the code PinmemberRancidCrabtree28-Mar-06 20:36 
QuestionRe: Explaination of the code PinmemberGlen_CodeProj29-Mar-06 18:27 
GeneralThanks you saved my day PinmemberDamir Valiulin23-Sep-05 6:43 
GeneralMy method PinmemberHughJampton10-May-05 3:24 
GeneralRe: My method PinmemberRancidCrabtree12-May-05 13:29 
GeneralRe: My method PinmemberSam NG18-Apr-06 0:29 
GeneralOne-line way of doing it Pinsussjocool13-Dec-04 4:37 
GeneralRe: One-line way of doing it PinmemberRancidCrabtree13-Dec-04 18:47 
GeneralThe way I do it PinsitebuilderUwe Keim30-Nov-04 20:51 
GeneralRe: The way I do it PinmemberRancidCrabtree1-Dec-04 8:38 
GeneralRe: The way I do it PinsitebuilderUwe Keim1-Dec-04 18:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 1 Dec 2004
Article Copyright 2004 by Geno Carman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid