Writing to the Internet Explorer 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值