Click here to Skip to main content
Click here to Skip to main content

Writing to the Internet Explorer Cache

, 28 Apr 2006
Rate this:
Please Sign up or sign in to vote.
An article on reading files from and writing files to Internet Explorer's cache.

Sample Image

Introduction

This article shows how to read a file from Internet Explorer's cache, and write a file to the cache.

Reading from the cache

Retrieving the local cache file name of a URL is simple. You call GetUrlCacheEntryInfo with the URL, and it will return the local file name. The function will return TRUE if the URL is in the cache, and FALSE otherwise. Then, you simply read the page and display it, or do whatever you want with it. Alternatively, you could use the function RetrieveUrlCacheEntryFile or RetrieveUrlCacheEntryStream to return the file or stream directly to your application.

CString strURL;
//get the url to read
m_url.GetWindowText(strURL);
//set up the cache entry
DWORD dwBufferSize = 4096;
LPINTERNET_CACHE_ENTRY_INFO lpCacheEntry = 
  (LPINTERNET_CACHE_ENTRY_INFO) new char[dwBufferSize];
lpCacheEntry->dwStructSize = dwBufferSize;
//retrieve the cache entry
if (GetUrlCacheEntryInfo(strURL,lpCacheEntry,
    &dwBufferSize) == TRUE)
{
    //navigate to the local page
    m_browser.Navigate(lpCacheEntry->lpszLocalFileName, 
                       NULL,NULL,NULL,NULL);
}
//delete the entry structure
delete lpCacheEntry;

Writing files to the cache

Writing files to the cache is a little more complicated. It requires a couple function calls. One method is to use URLDownloadToCacheFile. I am behind a proxy, and so URLDownloadToFile will not work for me. The method I use is just as simple. You first call CreateUrlCacheEntry with the URL and file type and a holder for the cache file name. The function will return a cache file name of where the URL is to be copied. Next, you write the data to the returned cache file. I use a simple local test page as the source of the file to be written, and copy its contents to the cache file. Finally, you call CommitUrlCacheEntry to finalize the entry into the cache. When calling this method, you specify the URL, the cache file name returned in CreateUrlCacheEntry, the expired and modified time if known, and a simple header. Use this header if none is known: (LPBYTE)"HTTP/1.0 200 OK\r\n\r\n".

CString strURL;
m_url.GetWindowText(strURL);
char lpszFileName[MAX_PATH+1];
//create a cache entry for the url
if (CreateUrlCacheEntry(strURL,0,"htm", 
    lpszFileName,0) == TRUE)
{
    /* //This would save to a local file
    //I did not use this function because I am behind a proxy
    //and the webcontrol does not work from behind a proxy
    //or if it does I could not find out how to make it work
    URLDownloadToFile(0,strURL,lpszFileName,0,0);
    //Alternatively you could skip everything after this
    //and just use URLDownloadToCacheFile() */


    //But we copy the local file to the cache file
    char ch;

    //Open the file for reading.
    ifstream fp_read("testme.html", ios_base::in);
    //open the cache file for writing
    ofstream fp_write(lpszFileName, ios_base::out);

    while(fp_read.eof() != true)
    {
        fp_read.get(ch);
        //Check for CR (carriage return)
        if((int)ch == 0x0D)
            continue;
        if (!fp_read.eof())fp_write.put(ch);
    }
    fp_read.close();
    fp_write.close();
    //create a null expire and modified time
    FILETIME ftExpireTime;
    ftExpireTime.dwHighDateTime = 0;
    ftExpireTime.dwLowDateTime = 0;
    FILETIME ftModifiedTime;
    ftModifiedTime.dwHighDateTime = 0;
    ftModifiedTime.dwLowDateTime = 0;
    //commit the cache entry
    //the HTTP/1.0 200 OK\r\n\r\n should be 
    //used for the header or else IE will not read it
    if (CommitUrlCacheEntry(strURL,lpszFileName, 
                ftExpireTime,ftModifiedTime,
                NORMAL_CACHE_ENTRY,
                (LPBYTE)"HTTP/1.0 200 OK\r\n\r\n", 
                20, NULL, NULL) == TRUE)
    {
        //Navigate to the local file
        m_browser.Navigate(lpszFileName,NULL, 
                             NULL,NULL,NULL);
    }
}

That's all there is to it!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Fred Ackers
Web Developer
United States United States
Programming using MFC and ATL for almost 12 years now. Currently studying Operating System implementation as well as Image processing. Previously worked on DSP and the use of FFT for audio application. Programmed using ADO, ODBC, ATL, COM, MFC for shell interfacing, databasing tasks, Internet items, and customization programs.

Comments and Discussions

 
GeneralAcknowledgment PinmemberDeza_24-Feb-08 7:42 
Generalhaving trobles..... PinmemberSteve Guerreri20-Jul-07 13:12 
GeneralRe: having trobles..... PinmemberFred Ackers26-Jul-07 8:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411023.1 | Last Updated 28 Apr 2006
Article Copyright 2006 by Fred Ackers
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid