//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);// Send a request.if (hRequest) bResults = WinhttpSendRequest( hRequest,WINhttp_NO_ADDITIONAL_headerS,WINhttp_NO_REQUEST_DATA,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 fileofstream out("test.txt",ios::binary);for (int i = 0; i < (int) vfileContent.size();i++)out << vfileContent[i];out.close();
当我尝试下载图片时,我只得到文件的第一行,没有错误信息.问题似乎与WinhttpOpenRequest函数中的此参数(ppwszAcceptTypes)有关.
link text
解决方法 看起来MSDN上的这个线程是一样的,并有解决方案http://social.msdn.microsoft.com/forums/en-US/vclanguage/thread/45ccd91c-6794-4f9b-8f4f-865c76cc146d
总结以上是内存溢出为你收集整理的如何在C/C++中使用WinHTTP下载文件?全部内容,希望文章能够帮你解决如何在C/C++中使用WinHTTP下载文件?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)