如何在C / C ++中用WinHTTP下载文件?

我知道如何下载一个HTML / TXT页面。 例如 :

//Variables DWORD dwSize = 0; DWORD dwDownloaded = 0; LPSTR pszOutBuffer; vector <string> 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, &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, &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>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 < (int) vFileContent.size();i++) out << vFileContent[i]; out.close(); 

当我尝试下载图片时,我只收到文件的第一行,没有错误信息。 这个问题似乎与WinHttpOpenRequest函数中的这个参数(ppwszAcceptTypes)有关。

链接文本

Solutions Collecting From Web of "如何在C / C ++中用WinHTTP下载文件?"

看起来像MSDN上的这个线程是一样的,并有解决方案

http://social.msdn.microsoft.com/forums/en-US/vclanguage/thread/45ccd91c-6794-4f9b-8f4f-865c76cc146d

解答:

 FILE * pFile; // NEW pFile = fopen("file.bin", "w+b"); // NEW if (bResults) do { // Check for available data. dwSize = 0; if (!WinHttpQueryDataAvailable( hRequest, &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, &dwDownloaded)) { printf( "Error %u in WinHttpReadData.\n", GetLastError()); } else { printf("%s", pszOutBuffer); fwrite(pszOutBuffer, (size_t)dwDownloaded, (size_t)1, pFile); // NEW } // Free the memory allocated to the buffer. delete [] pszOutBuffer; } } while (dwSize>0); fclose (pFile); // NEW 

仅仅以二进制模式打开流媒体并不会改变“运营商的工作方式 – 他们将始终执行格式化的输出。 你需要使用流的write()函数,它没有格式化输出。