MSDN中有一个网页下载的例子:tear,for VC++

解决方案 »

  1.   


    CString GetFileByHttp(const char *pstrHttpUrl, const char *pstrFileName)
    {
         #define HTTPBUFLEN    512 // Size of HTTP Buffer...     char httpbuff[HTTPBUFLEN];
         TCHAR   szCause[255];
         CString Cause;
         Cause.Format("YES");
     
         TRY
         {
              CInternetSession mysession;
              CStdioFile *remotefile = mysession.OpenURL(pstrHttpUrl,1,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_RELOAD);
     
              CFile myfile(pstrFileName, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
              int numbytes;
     
              while (numbytes = remotefile->Read(httpbuff, HTTPBUFLEN))
                   myfile.Write(httpbuff, numbytes);
         }
     
         CATCH_ALL(error)
         {
              error->GetErrorMessage(szCause,254,NULL);
              Cause.Format("%s",szCause);
         }
         END_CATCH_ALL;
     
         return (Cause);
    }