#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;
using namespace std;
struct WebCacheInfo
{
CString strOriPath;
CString strNewPath;
WebCacheInfo(const CString & str,const CString & str1)
{
strOriPath = str;
strNewPath = str1;
};
};
BOOL fun( vector<CString> &vUrls, map< CString,WebCacheInfo>& mapCaches)
{
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_strPath = m_lpUrlNamePath.Right( nLenth - nPos1);
CString m_strLinpath = _T("C:\\Users\\lenovo\\Desktop\\cache") + m_strPath;
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;
file = fopen("C:/Users/lenovo/Desktop/cache/cache.txt","ab+");
if (file != NULL)
fwrite(m_strAll, sizeof(TCHAR), m_strAll.GetLength(), file);
if (file != NULL)
{
fclose(file) ;
file = NULL;
}
// mapCaches.insert(make_pair("(i++",WebCacheInfo(lpCacheEntry->lpszLocalFileName,m_strLinpath)));
CopyFile( lpCacheEntry->lpszLocalFileName , m_strLinpath , FALSE);
}
}
if (lpCacheEntry != NULL)
{
delete []lpCacheEntry;
lpCacheEntry = NULL;
}
}
}
return TRUE;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
vector<CString> vUrls ;
map<CString ,WebCacheInfo> mapCaches;
vUrls.push_back(L"http://www.taobao.com/");
vUrls.push_back(L"https://www.baidu.com/");
vUrls.push_back(L"http://www.jd.com/");
fun(vUrls,mapCaches);
int nRetCode = 0;
//HMODULE hModule = ::GetModuleHandle(NULL);
//DWORD dwEntrySize = 0;
//LPINTERNET_CACHE_ENTRY_INFO lpCacheEntry;
//TCHAR m_lpUrlName [] = _T("http://www.taobao.com/"); //https://www.alipay.com/
//if (!GetUrlCacheEntryInfo( m_lpUrlName, NULL, &dwEntrySize))
//{
// if (GetLastError()!=ERROR_INSUFFICIENT_BUFFER)
// return FALSE;
// else
// 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_strPath = m_lpUrlNamePath.Right(nLenth - nPos - lstrlen(_T("C:")));
// CString m_strPath = m_lpUrlNamePath.Right( nLenth - nPos1);
// CString m_strLinpath = _T("C:\\Users\\newbie\\Desktop\\cache") + m_strPath;
// 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;
// file = fopen("C:/Users/newbie/Desktop/cache/cache.txt","ab+");
// if (file != NULL)
// fwrite(m_strAll, sizeof(TCHAR), m_strAll.GetLength(), file);
// if (file != NULL)
// { fclose(file) ; file = NULL;}
// CopyFile( lpCacheEntry->lpszLocalFileName , m_strLinpath , FALSE);
// }
// }
// if (lpCacheEntry != NULL)
// {
// delete []lpCacheEntry;
// lpCacheEntry = NULL;
// }
//}
system("pause");
return nRetCode;
}