登 录
首先包含头文件声明变量
#include <afxinet.h> public: //目标网址 CString url; //定义网络连接Session CInternetSession localSession; //定义一个文件对象指针 CInternetFile* webFile; //网页源代码 CString sourceCode;
然后实现代码
void CDownLoadFile::StartDownLoad() { webFile=NULL; //如果未加http://则自动添加 if (url.Left(7)!="http://") { url="http://"+url; } //打开连接 try { webFile=(CInternetFile*)localSession.OpenURL(url); } //捕获异常 catch (CInternetException* pException) { webFile=NULL; pException->Delete(); } if (webFile) { //删除以前保留的文件 DeleteFile("./url//temp.txt"); //将源代码写入文件 ofstream out; out.open("./url//temp.txt",ios::out); //读取页面源代码 while (webFile->ReadString(sourceCode)) { out<<sourceCode<<endl; } out.close(); out.clear(); } //删除文件指针 delete webFile; }
但是由于某些网站的速度很慢,用这种方法有时根本下载不下来,但是苦于找不到替代办法,所以一直用这个来实现了!
抱歉!评论已关闭.