// 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;
}
//
#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;
}