This Code uses the WinInet class to directly open a session, read and save the webpage.
BOOL CDdDlg: 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; // You can also use the LPTSTR type. The \ n carriage return in the text will not be deleted.
CString strPath;
GetModuleFileName (NULL, strPath. GetBufferSetLength (MAX_PATH + 1), MAX_PATH );
StrPath. ReleaseBuffer ();
Int nPos;
NPos = strPath. ReverseFind ('\\');
StrPath = strPath. Left (nPos );
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) // If 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 ("\ n"); // If somecode uses the LPTSTR type, skip this sentence.
}
File-> Close ();
Delete file;
}
Else
{
DataStore. WriteString (_ T ("failed to establish connection to the specified server ..."));
Return FALSE;
}
Return TRUE;
}
You only need to add the following header file to apply this function.
# Include "afxinet. h" // Add the header file to be used to download the webpage
The first parameter of GetSourceHtml (CString theUrl, CString Filename) is the URL to be downloaded, and the second parameter is the file name to be saved.
Author: Li Mu Space"