Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
BOOL Download(CString& m_strDownLoadURL,CString& m_strDownLoadDesFileName)
{
        ASSERT(m_strDownLoadURL != "");
	ASSERT(m_strDownLoadDesFileName != "");
        CInternetSession session;
	CHttpConnection* pHttpConnection = NULL;
	CHttpFile* pHttpFile = NULL;
	CString strServer, strObject;
	INTERNET_PORT wPort;
	DWORD dwType;
	const int nTimeOut = 2000;
	session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); 	session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);  
	char* pszBuffer = NULL; 
        	try
	       {
		AfxParseURL(m_strDownLoadURL, dwType, strServer, strObject, wPort);
		pHttpConnection = session.GetHttpConnection(strServer, wPort);
		pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject);
		if(pHttpFile->SendRequest() == FALSE)
			return false;
		DWORD dwStateCode;
               pHttpFile->QueryInfoStatusCode(dwStateCode);
		if(dwStateCode == HTTP_STATUS_OK)
		{
		  HANDLE hFile = CreateFile(m_strDownLoadDesFileName, GENERIC_WRITE,
				FILE_SHARE_WRITE, NULL,                              CREATE_ALWAYS,                  FILE_ATTRIBUTE_NORMAL,
				NULL);  
			if(hFile == INVALID_HANDLE_VALUE)
			{
				pHttpFile->Close();
				pHttpConnection->Close();
				session.Close();
				return false;
			}

                }
              
			char szInfoBuffer[1000]; 
			DWORD dwFileSize = 0;   
			DWORD dwInfoBufferSize = sizeof(szInfoBuffer);
			BOOL bResult = FALSE;
			bResult = pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,
			(void*)szInfoBuffer,&dwInfoBufferSize,NULL);


                        dwFileSize = atoi(szInfoBuffer);
			const int BUFFER_LENGTH = 1024 * 10;
			pszBuffer = new char[BUFFER_LENGTH];  			                  DWORD dwWrite, dwTotalWrite;
			dwWrite = dwTotalWrite = 0;
			UINT nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH);
                        
                        while(nRead > 0)
			{
			    WriteFile(hFile, pszBuffer, nRead, &dwWrite, NULL);  
			    dwTotalWrite += dwWrite;
			    nRead = pHttpFile->Read(pszBuffer, BUFFER_LENGTH);
			}

                        delete[]pszBuffer;
			pszBuffer = NULL;
			CloseHandle(hFile);
              catch(...)
             {
                // do something
             }
Posted

1 solution

If you have retrieved the data once with active internet connection it is cached. If you try to retrieve the same data again, you might get a copy from the cache.

To force a new download, use the flag INTERNET_FLAG_RELOAD when calling CHttpConnection::OpenRequest().
 
Share this answer
 
Comments
tygeztt 1-Nov-12 3:20am    
Thanks ,when I use the flag INTERNET_FLAG_RELOAD ,calling pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject,NULL,1,NULL,NULL,INTERNET_FLAG_RELOAD | INTERNET_FLAG_EXISTING_CONNECT) or pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject,NULL,1,NULL,NULL,INTERNET_FLAG_RELOAD ),will report Debug Assert Failed in the line 2302 of the inet.cpp,but I can not download file when disconnect the network.How can I slove the Debug Assert Failed? Looking forward to your answer
tygeztt 1-Nov-12 3:36am    
Hello,I do it with the code below:
pHttpConnection = session.GetHttpConnection(strServer, wPort);
BOOL b = InternetSetOption(session,INTERNET_FLAG_RELOAD,NULL,0);
pHttpFile = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject);
Thank you very much!best wishes
Jochen Arndt 1-Nov-12 3:48am    
Fine that it is working now.

I assume the assertion was sourced by the failure of connecting to the internet. But GetHttpConnection() should throw a CInternetException when connecting fails.
tygeztt 1-Nov-12 4:02am    
The strange thing is when I ignore the debug assert failed ,still download file if keep connecting to the internet.so ,I think it is successful of connecting to the internet,how about my opinion?thanks
Jochen Arndt 1-Nov-12 4:42am    
You are passing INTERNET_FLAG_RELOAD to InternetSetOption() which uses INTERNET_OPTION_xxx flags.

You should debug your application to see why the assertion is fired. Check what asserted (e.g. m_hConnection) and trace back the call stack or do it manually using your sources and inet.cpp.

To avoid the assertion you should check for an active internet connection. If there is no connection, (HINTERNET)*pHttpConnection is NULL.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900