使用CInternetSession和CHttpFile读取网页内容
参考网址: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中的内容。