title: How to download a file with WinHTTP in C/C++?
body: <p>I know how to download an html/txt page. For example :</p>
  
  <pre><code>//Variables 
  DWORD dwSize = 0;
  DWORD dwDownloaded = 0;
  LPSTR pszOutBuffer;
  vector &lt;string&gt;  vFileContent;
  BOOL  bResults = FALSE;
  HINTERNET  hSession = NULL, 
             hConnect = NULL,
             hRequest = NULL;
  
  // Use WinHttpOpen to obtain a session handle.
  hSession = WinHttpOpen( L"WinHTTP Example/1.0",  
                          WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                          WINHTTP_NO_PROXY_NAME, 
                          WINHTTP_NO_PROXY_BYPASS, 0);
  
  // Specify an HTTP server.
  if (hSession)
      hConnect = WinHttpConnect( hSession, L"nytimes.com",
                                 INTERNET_DEFAULT_HTTP_PORT, 0);
  
  // Create an HTTP request handle.
  if (hConnect)
      hRequest = WinHttpOpenRequest( hConnect, L"GET", L"/ref/multimedia/podcasts.html",
                                     NULL, WINHTTP_NO_REFERER, 
                                     NULL, 
                                     NULL);
  
  // Send a request.
  if (hRequest)
      bResults = WinHttpSendRequest( hRequest,
                                     WINHTTP_NO_ADDITIONAL_HEADERS,
                                     0, WINHTTP_NO_REQUEST_DATA, 0, 
                                     0, 0);
  
  
  // End the request.
  if (bResults)
      bResults = WinHttpReceiveResponse( hRequest, NULL);
  
  // Keep checking for data until there is nothing left.
  if (bResults)
      do 
      {
  
          // Check for available data.
          dwSize = 0;
          if (!WinHttpQueryDataAvailable( hRequest, &amp;dwSize))
              printf( "Error %u in WinHttpQueryDataAvailable.\n",
                      GetLastError());
  
          // Allocate space for the buffer.
          pszOutBuffer = new char[dwSize+1];
          if (!pszOutBuffer)
          {
              printf("Out of memory\n");
              dwSize=0;
          }
          else
          {
              // Read the Data.
              ZeroMemory(pszOutBuffer, dwSize+1);
  
              if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, 
                                    dwSize, &amp;dwDownloaded))
              {
                  printf( "Error %u in WinHttpReadData.\n", 
                          GetLastError());
              }
              else
              {
                          printf("%s", pszOutBuffer);
                              // Data in vFileContent
                  vFileContent.push_back(pszOutBuffer);
              }
  
              // Free the memory allocated to the buffer.
              delete [] pszOutBuffer;
          }
  
      } while (dwSize&gt;0);
  
  
  // Report any errors.
  if (!bResults)
      printf("Error %d has occurred.\n",GetLastError());
  
  // Close any open handles.
  if (hRequest) WinHttpCloseHandle(hRequest);
  if (hConnect) WinHttpCloseHandle(hConnect);
  if (hSession) WinHttpCloseHandle(hSession);
  
  // Write vFileContent to file
  ofstream out("test.txt",ios::binary);
  for (int i = 0; i &lt; (int) vFileContent.size();i++)
  out &lt;&lt; vFileContent[i];
  out.close();
  </code></pre>
  
  <p>When I try to download a picture, I get only the first lines of the file and no error message. The problem seems related to this parameter (ppwszAcceptTypes) in WinHttpOpenRequest Function. </p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/aa384099(VS.85).aspx" rel="noreferrer">link text</a></p>