How to overcome caching issue when downloading data from internet using CHttpFile

Viewed 47

This is how I currently download a ZIP file from the internet in my MFC project:

strTargetZIP = theApp.GetWorkingPath() + _T("AutoUpdate\\MWBDataUpdate.zip");
strDownloadURL = _T("https://www.publictalksoftware.co.uk/mwbdata/MWBDataUpdate.zip");

// ask user to go on line
const BOOL bOnline = InternetGoOnline(strDownloadURL.GetBuffer(), hWnd, 0);
strDownloadURL.ReleaseBuffer();

if (!bOnline)
    return false;

try
{
    // our session should already be open
    // try to open up Internet session to my URL
    // Use flag INTERNET_FLAG_RELOAD
    pWebFile = dynamic_cast<CHttpFile*>(iSession.OpenURL(strDownloadURL, 1,
    INTERNET_FLAG_SECURE | INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_RELOAD));

    if (pWebFile != nullptr)
    {
        if (pWebFile->QueryInfoStatusCode(dwStatusCode))
        {
            // 20x codes mean success
            if ((dwStatusCode / 100) == 2)
            {
                if (fileLocal.Open(strTargetZIP,
                    CFile::modeCreate | CFile::modeWrite | CFile::typeBinary))
                {
                    iRead = pWebFile->Read(&szBuffer[0], 4096);
                    while (iRead > 0)
                    {
                        iBytesRead += iRead;
                        fileLocal.Write(&szBuffer[0], iRead);
                        iRead = pWebFile->Read(&szBuffer[0], 4096);
                    }
                    fileLocal.Close();

                    bOK = true;
                }
            }
            else
            {
                // There was a problem!
                strError.Format(IDS_TPL_INVALID_URL, dwStatusCode);
                AfxMessageBox(strError, MB_OK | MB_ICONERROR);
            }
        }
    }
    else
    {
        // Note, there is no error log.  Use new error message?
        AfxMessageBox(IDS_STR_UPDATE_CHECK_ERR, MB_OK | MB_ICONERROR);
    }
}
catch(CException *e_)
{
    const gsl::not_null<CException*> e{ e_ };
    e->ReportError();
    e->Delete();
}

As you can see, I have tried to cater for caching issues. However, it seems that on occasion, for some users, the software does not download the latest ZIP and uses a cached version.

What further steps can I as a developer (or they as a user) take to ensure that my software does actually download the latest ZIP file on my website server?

0 Answers
Related