CHttpFile抓取网页源代码

CHttpFile抓取网页源代码

发现MFC真是一个好东西,减少许多C++开发者造轮子的麻烦(说MFC-微软基础类库过时的勿喷)。如下,包含头文件afxinet.h(该类封装了一些MFC SOCKET的操作)。CHttpFile抓取网页源代码:

[cpp]CInternetSession session(“HttpClient”);
char * url = “https://cpp.cloudcpp.com”;
CHttpFile* pfile = (CHttpFile *)session.OpenURL(url);

DWORD dwStatusCode;
pfile -> QueryInfoStatusCode(dwStatusCode);
if(dwStatusCode == HTTP_STATUS_OK)
{
CString content;
CString data;
while (pfile -> ReadString(data))
{
content += data + “\r\n”;
}
content.TrimRight();
printf(” %s\n ” ,(LPCTSTR)content);
}
pfile -> Close();
delete pfile;
session.Close();[/cpp]

来源:humin.blog.chinaunix.net