您的位置:首页 > 其它

VC获取cookies的几种方法

2017-03-25 00:00 197 查看
方法一:

CInternetSession::GetCookie

This member function implements the behavior of the Win32 function InternetGetCookie, as described in the Windows SDK.

static BOOL GetCookie(
LPCTSTR pstrUrl,
LPCTSTR pstrCookieName,
LPTSTR pstrCookieData,
DWORD dwBufLen
);
static BOOL GetCookie(
LPCTSTR pstrUrl,
LPCTSTR pstrCookieName,
CString& strCookieData
);


http://msdn.microsoft.com/en-us/library/vstudio/cff9kt47(v=vs.120).aspx

实现:

char * pszURL = "http://www.baidu.com/";
CInternetSession::GetCookie(pszURL, "", strCookie);
printf("%s\n", strCookie);


方法二:

InternetGetCookie

C++

BOOL InternetGetCookie(
_In_     LPCTSTR lpszUrl,
_In_     LPCTSTR lpszCookieName,
_Out_    LPTSTR lpszCookieData,
_Inout_  LPDWORD lpdwSize
);



http://msdn.microsoft.com/en-us/library/ie/aa384710(v=vs.85).aspx

实现:

LPDWORD lpdwSize = new DWORD;
char strCookie_two[100] = {0};
InternetGetCookie(pszURL, NULL, strCookie_two, lpdwSize);
InternetGetCookie(pszURL, NULL, strCookie_two, lpdwSize);
printf("%s\n", strCookie_two);


方法三:

QueryInfo

CInternetSession session("HttpClient");
CHttpFile* pfile = (CHttpFile *)session.OpenURL(pszURL);
CString strCookie_three;
pfile->QueryInfo(HTTP_QUERY_SET_COOKIE, strCookie_three);
printf("%s\n", strCookie_three);


Managing Cookies

http://msdn.microsoft.com/ZH-CN/library/windows/desktop/aa385326

//获取cookies的几种方法
#include <afxinet.h>
#include <atlstr.h>
#include <cstdio>

int  main()
{
char * pszURL = "http://blog.csdn.net/x_iya";

//方法一
printf("方法一:\n");
CString strCookie_one;
CInternetSession::GetCookie(pszURL, "", strCookie_one);
printf("%s\n", strCookie_one);

//方法二
printf("方法二:\n");
LPDWORD lpdwSize = new DWORD;
char strCookie_two[100] = {0};
InternetGetCookie(pszURL, NULL, strCookie_two, lpdwSize);
InternetGetCookie(pszURL, NULL, strCookie_two, lpdwSize);
printf("%s\n", strCookie_two);

//方法三
printf("方法三:\n");
CInternetSession session("HttpClient");
CHttpFile* pfile = (CHttpFile *)session.OpenURL(pszURL);
CString strCookie_three;
pfile->QueryInfo(HTTP_QUERY_SET_COOKIE, strCookie_three);
printf("%s\n", strCookie_three);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: