用WinInet下载http页面

来源:互联网 发布:青岛关键词seo排名 编辑:程序博客网 时间:2024/04/27 14:18
CInternetSession session;
 CInternetFile *pFile = NULL;
 try{
  pFile = (CInternetFile*)session.OpenURL("http://www.baidu.com");  //打开链接
 }catch(CInternetException *pEx){
  pEx->ReportError();
  pFile = NULL;
  pEx->Delete();
 }
 if(pFile)
 {
  char *buf = new char[9999];    
  UINT nCount = pFile->Read(buf,9999);   //把网页数据读入缓冲
   HANDLE hFile = CreateFile("baidu.html",GENERIC_WRITE,0,NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,0);        //创建文件
  if(INVALID_HANDLE_VALUE == hFile)
  {
   MessageBox("创建文件失败!");
  }
  else
  {
   CFile file((int)hFile);
   file.Write(buf,nCount);      //把缓冲中的数据写入文件
  }
  delete buf;
 }
 session.Close();
原创粉丝点击