Windows/MFC

홈페이지 소스 가져오기

aucd29 2013. 10. 2. 18:19
http://www.debuglab.com/knowledge/gethtmlsource.html

BOOL GetSourceHtml(CString theUrl)
{
    // this first block does the actual work
    CInternetSession session;
    CInternetFile* file = NULL;

    try
    {
        // try to connect to the URL
        file = (CInternetFile*) session.OpenURL(theUrl);
    }

    catch (CInternetException* m_pException)
    {
        // set file to NULL if there's an error
        file = NULL;
        m_pException->Delete();
    }

    // most of the following deals with storing the html to a file
    CStdioFile dataStore;
    if (file)
    {
        CString somecode;
        BOOL bIsOk = dataStore.Open(_T("C:\\rawHtml.txt"),
            CFile::modeCreate
            | CFile::modeWrite
            | CFile::shareDenyWrite
            | CFile::typeText);

        if (!bIsOk)
            return FALSE;

        // continue fetching code until there is no more
        while (file->ReadString(somecode) != NULL)
        {
            dataStore.WriteString(somecode);
        }

        file->Close();
        delete file;
    }
    else
    {
        dataStore.WriteString(_T("Could not establish a connection with the server..."));    
    }
}