HOWTO: Using InternetReadFile To Get File

Last reviewed: October 31, 1997
Article ID: Q149413
The information in this article applies to:
  • Microsoft ActiveX SDK, version 1.0
  • Microsoft Internet Information Server 3.0
  • Microsoft Windows NT 4.0
  • Microsoft Windows NT 3.51 with Service Pack 4 (SP4)
  • Microsoft Windows 95

SUMMARY

Using a WinInet API, the InternetReadFile returns the following buffer: "<body><h1>HTTP/1.0 406 No acceptable objects were found</h1></body>"

MORE INFORMATION

This is an HTTP server specific error. Microsoft IIS may return this error when there is no proper Accept header ("Accept: */*") in the HTTP request. Such a header can be added with following APIs:

   InternetOpenUrl(),
   HttpSendRequest(),
   HttpAddRequestHeaders()

Note that other HTTP servers may or may not behave in the same fashion. The following code demonstrates how to transfer any type of file with WinInet APIs:

   BOOL GetFile (HINTERNET IN hOpen, // Handle from InternetOpen()
                 CHAR *szUrl,        // Full URL
                 CHAR *szFileName)   // Local file name
   {
       DWORD dwSize;
       CHAR   szHead[] = "Accept: */*\r\n\r\n";
       VOID * szTemp[25];
       HINTERNET  hConnect;
      FILE * pFile;

       if ( !(hConnect = InternetOpenUrl ( hOpen, szUrl, szHead,
             lstrlen (szHead), INTERNET_FLAG_DONT_CACHE, 0)))
       {
         cerr << "Error !" << endl;
           return 0;
       }

       if  ( !(pFile = fopen (szFileName, "wb" ) ) )
      {
           cerr << "Error !" << endl;
          return FALSE;
      }
       do
       {
          // Keep coping in 25 bytes chunks, while file has any data left.
          // Note: bigger buffer will greatly improve performance.
          if (!InternetReadFile (hConnect, szTemp, 50,  &dwSize) )
          {
              fclose (pFile);
             cerr << "Error !" << endl;
            return FALSE;
          }
          if (!dwSize)
              break;  // Condition of dwSize=0 indicate EOF. Stop.
          else
             fwrite(szTemp, sizeof (char), dwSize , pFile);
       }   // do
      while (TRUE);
      fflush (pFile);
      fclose (pFile);
      return TRUE;
   }

The same task can be accomplished with WinInet MFC classes in following manner:

   #include <afx.h>
   #include <afxinet.h>

   BOOL  GetFile (CHAR *szUrl, CHAR *szFileName)
   {
   TCHAR sz[1024];
   CInternetSession session (_T("MyTest agent"), 1,
     INTERNET_OPEN_TYPE_DIRECT);
   CStdioFile* pFile = NULL;
   CHAR   szHead[] = "Accept: */*\r\n\r\n";
       DWORD nRead;
   CFile myFile;
   CFileException fileException;

   if ( !myFile.Open (szFileName, CFile::modeCreate | CFile::modeReadWrite,
      &fileException ) )
   {
   cerr << "Can't open file: " << szFileName
    << " error = " << fileException.m_cause  <<"\n";
   return FALSE;
   }

   try
   {
   pFile = session.OpenURL (szUrl, 1, INTERNET_FLAG_RELOAD
   |INTERNET_FLAG_TRANSFER_BINARY,
    szHead, -1L);

   }
   catch (CInternetException *pEx)
   {
   cerr <<"OpenUrl failed: "<< pEx-> m_dwError << endl;
   return FALSE;
   }

       do
   {
   nRead = pFile->Read(sz, 1023);
   if (nRead != 0)
   myFile.Write (sz, nRead);
   }
   while (nRead != 0);
   myFile.Close();
   pFile->Close();
   if (pFile != NULL)
   delete pFile;
   session.Close();
   return TRUE;
   }
Keywords          : AXSDKWinInet kbnetwork kbprg
Technology        : kbInetDev
Version           : 1.0 3.5 4.0 95
Platform          : NT WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 31, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.