浏览器cookies 和其路径获取

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

#include "stdafx.h"
#include "Testcookies0325.h"

#include <Windows.h>
#include <Stdio.h>
#include <Tchar.h>
#include <Wininet.h>
#pragma comment(lib, "Wininet.lib")
#include<map>
#include<vector>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// The one and only application object
CWinApp theApp;
using namespace std;
struct WebCookiesInfo
{
    CString strOriPath;
    CString strNewPath;
    WebCookiesInfo(const CString & str,const CString & str1)
    {
        strOriPath = str;
        strNewPath = str1;
    }
};






//获取Cookie的名称
BOOL CookieGetUrlName(LPTSTR lpszUrl, LPTSTR lpszUrlName)
{
  TCHAR        szUserName[32];
  DWORD        cbUserName = 32;
  TCHAR        szHostName[128];
  URL_COMPONENTS    urlComponents;
 
  if (!GetUserName(szUserName, &cbUserName))             //获取当前用户名)
  {
      cout<<"Error"<< GetLastError()<<endl;
      return FALSE;
  }
  memset(&urlComponents, 0, sizeof(urlComponents));
  urlComponents.dwStructSize = sizeof(urlComponents);
  urlComponents.dwHostNameLength = 128;                 //主机名长度
  urlComponents.lpszHostName = szHostName;              //主机名

  if(InternetCrackUrl(lpszUrl, lstrlen(lpszUrl), ICU_ESCAPE, &urlComponents))   //解析Url获得cookies的名称
  {
    lstrcpy(lpszUrlName, _T("Cookie:"));
    lstrcat(lpszUrlName, szUserName);
    lstrcat(lpszUrlName, _T("@"));
    lstrcat(lpszUrlName, szHostName);
    lstrcat(lpszUrlName, _T("/"));
    return TRUE;
  }else{
      cout << "Error"<<GetLastError()<<endl;
      return FALSE;
  }
  return FALSE;
 }





//获取Cookie的路径
BOOL CookieGetPath(LPTSTR lpszUrl, LPTSTR lpszFilePath)
{
  BOOL            bRet;
  HANDLE            hEnumHandle;
  LPINTERNET_CACHE_ENTRY_INFO  lpCacheEntry;
  DWORD            cbCacheEntry;
  TCHAR            szCookieUrlName[256];
  bRet = FALSE;

  CookieGetUrlName(lpszUrl, szCookieUrlName);//获取Cookie的名称
  cbCacheEntry = 10240;
  lpCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO)LocalAlloc(LMEM_FIXED, cbCacheEntry);

  hEnumHandle = FindFirstUrlCacheEntry(_T("cookie:"), lpCacheEntry, &cbCacheEntry);//查找Cookie缓存
  if(hEnumHandle)
  {
    do
    {
      if(lpCacheEntry->CacheEntryType & COOKIE_CACHE_ENTRY)
      {
        if(lstrcmpi(lpCacheEntry->lpszSourceUrlName, szCookieUrlName) == 0)//判断是否是我们要找的Cookie名
        {
          if (NULL == lstrcpy(lpszFilePath, lpCacheEntry->lpszLocalFileName)  )//复制Cookie的文件路径)
          {
              cout<< "拷贝cookiesde路径失败"<<endl;      return FALSE;
          }

           bRet = TRUE;
          break;
        }
      }
      cbCacheEntry = 10240;
    }
    while(FindNextUrlCacheEntry(hEnumHandle, lpCacheEntry, &cbCacheEntry));//下一个
    FindCloseUrlCache(hEnumHandle);//关闭句柄
  }
  LocalFree(lpCacheEntry);
  return bRet;
}






//获取cookies 路径
BOOL FindPath( vector<CString> &vUrls, map< CString,WebCookiesInfo>& mapCookies ,CString m_strFilePath )
{
    for( int i = 0; i < vUrls.size(); i++ )
    {
        CString str = vUrls[i];
        TCHAR    lpurl[150];
        _tcscpy(lpurl, str);

        DWORD    dwCookieField = 150;
        DWORD    dwCookieDataSize = 2000;
        TCHAR    lpfilename[150],lpfilepath[150];
        if (CookieGetUrlName(lpurl,lpfilename ))                       // 获取cookies名
        {
            BOOL m_Bool = CookieGetPath(lpurl,lpfilepath);            //获取cookies的路径名
            if (m_Bool)
            {
                CString m_lpUrlNamePath  = lpfilepath;
                int nLenth = 0;
                nLenth = lstrlen(m_lpUrlNamePath);
                int nPos1 = m_lpUrlNamePath.ReverseFind('\\');
                CString m_strFileName = m_lpUrlNamePath.Right( nLenth - nPos1);
                CString m_strTempPath = m_strFilePath + m_strFileName;

                CString  str;
                str.Format(_T("%s"),lpurl);
                CString m_strAll = str + (L"\r\n")+ m_lpUrlNamePath +(L"\r\n")+ (L"\r\n");
                FILE *file = NULL;
                CString m_str = m_strFilePath + _T("\\cookies.txt" );
                char szStr[50] = {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;
                }
                mapCookies.insert(make_pair(vUrls[i] ,WebCookiesInfo(lpfilepath,m_strTempPath)));  //插入数据
                /*map<CString,WebCookiesInfo>::iterator it;
                for(it=mapCookies.begin();it!=mapCookies.end();++it)                              // 遍历map
                {
                    wcout<<"key: "<< it->first.GetString()<<endl <<" valueOripath: "<< it->second.strOriPath.GetString()<< endl
                    <<"valueNewpath:"<<it->second.strNewPath.GetString()<<endl;
                }*/
                if (!CopyFile( lpfilepath , m_strTempPath , FALSE))     //文件复制
                {
                    cout<<"拷贝文件出错"<<endl;    return FALSE;
                }
            
            }else{
                cout<<"获取cookies路径失败"<< i+1<<endl;
                return FALSE;
            }
        }else{
            cout<<"获取cookies名失败"<< i+1<<endl;
            return FALSE;
        }
    }
    return TRUE;
}





int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;
    HMODULE hModule = ::GetModuleHandle(NULL);
    vector<CString> vUrls ;
    map<CString ,WebCookiesInfo> mapCookies;
    vUrls.push_back(L"http://www.taobao.com/");
    vUrls.push_back(L"https://www.baidu.com/");
    vUrls.push_back(L"http://ai.taobao.com/?pid=mm_26632323_6762370_25910879");
    CString m_strFilePath = _T("C:\\Users\\newbie\\Desktop\\cookies");
    FindPath(vUrls,mapCookies,m_strFilePath);
    system("pause");
    return nRetCode;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值