Basic Idea: Use the WinInet class to directly open a session, read and save the webpage to related files.
The implementation number is as follows:
BOOL GetSourceHtml (CString theUrl, CString Filename)
{
CInternetSession session;
CInternetFile * file = NULL;
Try
{
// Try to connect to the specified URL
File = (CInternetFile *) session. OpenURL (theUrl );
}
Catch (CInternetException * m_pException)
{
// If any error occurs, leave the file blank.
File = NULL;
M_pException-> Delete ();
Return FALSE;
}
// Use dataStore to save the webpage Files Read
CStdioFile dataStore;
If (file)
{
CString somecode;File: // alsoThe LPTSTR type can be used. Carriage Return Characters in the text will not be deleted.
BOOL bIsOk = dataStore. Open (strPath + "\" + Filename,
Cfile: modeCreate
| Cfile: modeWrite
| Cfile: shareDenyWrite
| Cfile: typeText );
If (! BIsOk)
Return FALSE;
// Read and write the webpage file until it is empty
While (file-> ReadString (somecode )! = NULL)File: // suchIf the LPTSTR type is used, the maximum number of nMax reads is set to 0, so that it ends when it encounters a null character.
{
DataStore. WriteString (somecode );
DataStore. WriteString ("");File: // suchIf the somecode type is LPTSTR, this sentence is not required.
}
File-> Close ();
Delete file;
}
Else
{
DataStore. WriteString (_ T ("failed to establish connection to the specified server ..."));
Return FALSE;
}
Return TRUE;
}
Let's take a look at how to use it:
1. Add the WinInt class as follows:
# Include "afxinet. h"File: // AddHeader files to be downloaded
2. After adding the downloaded content to your project, you can use the following code (the first parameter is the URL, and the second parameter is the file name saved after the download ):
File: // getTake the path of the main program, which exists in the global variable strPath.
GetModuleFileName (NULL, strPath. GetBufferSetLength (MAX_PATH + 1), MAX_PATH );
StrPath. ReleaseBuffer ();
Int nPos;
NPos = strPath. ReverseFind (\);
StrPath = strPath. Left (nPos );
BOOL m_bDownloadFailed; m_bDownloadFailed = GetSourceHtml ("http://www.vckbase.com", "News.txt ");File: //Contains the default URL of the prompt File