參考網址:http://www.398588.com/Article/T9/78.aspx
讀取網頁的內容可以類比本地的檔案一下,程式碼如下:
#include <afxinet.h>
CString url;
GetDlgItemText(IDC_EDIT1,url);
CInternetSession Sess;
CHttpFile* cFile = (CHttpFile*)Sess.OpenURL(url,1,INTERNET_FLAG_TRANSFER_ASCII||INTERNET_FLAG_RELOAD,NULL,0);
DWORD dwStatusCode;
cFile->QueryInfoStatusCode(dwStatusCode);
if(dwStatusCode == HTTP_STATUS_OK)
{
CString szData,szAllData;
while(cFile->ReadString(szData))
{
szAllData += szData;
szAllData += "\r\n";
}
cFile->Close();
Sess.Close();
CString name ;
name = "sd.htm";
CFile file(name, CFile::modeCreate | CFile::modeWrite);
file.Write(szAllData,szAllData.GetLength());
file.Close();
}
else
{
MessageBox("請求失敗。。。。");
}
GetDlgItemText(IDC_EDIT1,url);
CInternetSession Sess;
CHttpFile* cFile = (CHttpFile*)Sess.OpenURL(url,1,INTERNET_FLAG_TRANSFER_ASCII||INTERNET_FLAG_RELOAD,NULL,0);
DWORD dwStatusCode;
cFile->QueryInfoStatusCode(dwStatusCode);
if(dwStatusCode == HTTP_STATUS_OK)
{
CString szData,szAllData;
while(cFile->ReadString(szData))
{
szAllData += szData;
szAllData += "\r\n";
}
cFile->Close();
Sess.Close();
CString name ;
name = "sd.htm";
CFile file(name, CFile::modeCreate | CFile::modeWrite);
file.Write(szAllData,szAllData.GetLength());
file.Close();
}
else
{
MessageBox("請求失敗。。。。");
}
用CInternetSession開啟url得到一個CHttpFile,用ReadString讀取CHttpFile中的內容。