VC获取cookies的几种方法

方法一:

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
", strCookie);

方法二:

InternetGetCookie

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
", 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
", strCookie_three);


Managing Cookies


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

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

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


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


	//方法三
	printf("方法三:
");
	CInternetSession session("HttpClient");
	CHttpFile* pfile = (CHttpFile *)session.OpenURL(pszURL);
	CString strCookie_three;
	pfile->QueryInfo(HTTP_QUERY_SET_COOKIE, strCookie_three);
	printf("%s
", strCookie_three);
    return 0;
}



Keep it simple!
作者:N3verL4nd
知识共享,欢迎转载。
原文地址:https://www.cnblogs.com/lgh1992314/p/5834884.html