How to download a file with WinHTTP in C/C++? -
I know how an html / txt page to download example:
// variable DWORD dwSize = 0; DWORD dwDownloaded = 0; LPPS pszOutBuffer; Vector & lt; String & gt; VFileContent; BOOL bResults = FALSE; Indicator h session = zero, hConnect = NULL, hRequest = NULL; // use WinHttpOpen to handle a session 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 (h session, l "nytimes.com", INTERNET_DEFAULT_HTTP_PORT, 0); // Create a HTTP request handled if (hConnect) hRequest = WinHttpOpenRequest (Acknekt, 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 request if (bResults) bResults = WinHttpReceiveResponse (hRequest, NULL); // Check for data until nothing left. If (bResults) do {// Check DwSize = 0 for available data; If (! WinHttpQueryDataAvailable (hRequest, & dwSize)) printf ( "Error% u WinHttpQueryDataAvailable. \ N", GetLastError ()); // Allocate space for buffer PszOutBuffer = new four [dwSize + 1]; If (PszOutBuffer) {printf ("Out of memory \ n"); DwSize = 0; } Else {// read data zeromemory (pszOutBuffer, dwSize + 1); If (! WinHttpReadData (hRequest, (LPVOID) pszOutBuffer, dwSize, and dwDownloaded)) {printf ( "Error% u WinHttpReadData. \ N", GetLastError ()); } Other {printf ("% s", pszOutBuffer); // vFileContent data in vFileContent.push_back (pszOutBuffer); } // Memory allocated to buffer free. Remove [pszOutBuffer; }} While (dwSize> 0); // Report any errors if (! BResults) printf ("Error% d has occurred. \ N", GetLastError ()); // Close any open handle if (Hrzest) WinHttpCloseHandle (hRequest); If (HK) Connect WinHttpCloseHandle (hConnect); If (h sessions) WinHttpCloseHandle (hSession); Write to vfilecontent's outstream file ("test.txt", ios :: binary); For (Int i = 0; I When I try to download a picture, I get only the first lines of the file and no error message is found. The problem is related to this parameter (ppwszcceptTypes) in the WinHttpOpenRequest function. It looks like this thread is the same on MSDN and it is the solution
Comments
Post a Comment