获取浏览器的 cache 和其路径

// TestCache.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "TestCache.h"
#include<WinInet.h>
#include<Windows.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#pragma comment(lib, "Wininet.lib")
#include<map>
#include<vector>
using namespace std;

// The one and only application object
CWinApp theApp;


struct WebCacheInfo
{
    CString strOriPath;
    CString strNewPath;
    WebCacheInfo(const CString & str,const CString & str1)
    {
        strOriPath = str;
        strNewPath = str1;
    }
};


BOOL FindCachePath( vector<CString> &vUrls, map< CString,WebCacheInfo>& mapCaches ,CString m_strFilePath)
{
    DWORD dwEntrySize = 0;
    LPINTERNET_CACHE_ENTRY_INFO lpCacheEntry ;
    for( int i = 0; i < vUrls.size(); i++ )
    {
        CString str = vUrls[i];
        TCHAR m_lpUrlName[100];
        _tcscpy(m_lpUrlName, str);

        if (!GetUrlCacheEntryInfo( m_lpUrlName, NULL, &dwEntrySize))
        {
            if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
                lpCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO)new BYTE[dwEntrySize];
            
            if (lpCacheEntry != NULL)
            {
                if (GetUrlCacheEntryInfo( m_lpUrlName,lpCacheEntry, &dwEntrySize))
                {
                    CString m_lpUrlNamePath  = lpCacheEntry->lpszLocalFileName;
                    int nLenth = 0;
                    nLenth = lstrlen(m_lpUrlNamePath);
                    int nPos1 = m_lpUrlNamePath.ReverseFind('\\');
                    CString m_strFileName = m_lpUrlNamePath.Right( nLenth - nPos1);     // 获取cache的文件名
                    CString m_strTempPath = m_strFilePath + m_strFileName;     

                    CString  str;  
                    str.Format(_T("%s"),m_lpUrlName);        // 类型转换
                    CString m_strAll = str + (L"\r\n")+ m_lpUrlNamePath +(L"\r\n")+ (L"\r\n");
                    FILE *file = NULL;
                    CString m_str = m_strFilePath + _T("\\cache.txt" );         //为创建txt文本指定文件路径
                    char szStr[80] = {0};
                    if (wcstombs(szStr, m_str, m_str.GetLength()) == -1)
                        return FALSE;

                    const char * fileName = szStr;
                    file = fopen(fileName,"ab+");         // 创建文件
                    if (file != NULL)
                    {
                        if ( fwrite(m_strAll, sizeof(TCHAR), m_strAll.GetLength(), file) != m_strAll.GetLength())
                        {        cout<<"写入文件出错"<<endl;  return FALSE;    }
                    }
                    else{
                        cout<<"创建文件出错"<<endl;
                        return FALSE;
                    }

                    if (file != NULL)
                    {
                        fclose(file) ;    file = NULL;
                    }
                    mapCaches.insert(make_pair(vUrls[i] ,WebCacheInfo(lpCacheEntry->lpszLocalFileName,m_strTempPath)));   //插入数据
                    /*map<CString,WebCacheInfo>::iterator it;    // map 的遍历
                    for(it=mapCaches.begin();it!=mapCaches.end();++it)
                    {                        
                        wcout<<"key: "<< it->first.GetString()<<" valueOripath: "<< it->second.strOriPath.GetString()<< endl<<"valueNewpath"<<it->second.strNewPath.GetString()<<endl;
                    }*/
                    if (!CopyFile( lpCacheEntry->lpszLocalFileName , m_strTempPath , FALSE))     //文件复制
                    {
                        cout<<"拷贝文件出错"<<endl;    return FALSE;
                    }
                }
            }
            if (lpCacheEntry != NULL)
            {
                delete []lpCacheEntry;
                lpCacheEntry = NULL;
            }
        }else{
            cout<<"获取cahce名失败"<< i+1<<endl;
            return FALSE;
        }
    }
    return TRUE;
}



int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;
    vector<CString>  vUrls ;
    map<CString ,WebCacheInfo> mapCaches;
    vUrls.push_back(L"http://www.taobao.com/");  // 插入数据
    vUrls.push_back(L"https://financeprod.alipay.com/fund/index.htm");
    vUrls.push_back(L"https://www.baidu.com/");
    CString m_strFilePath = _T("C:\\Users\\newbie\\Desktop\\cache");    // 指定的本地路径
    FindCachePath(vUrls,mapCaches,m_strFilePath);
    system("pause");
    return nRetCode;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值