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 );
实现:
char * pszURL = "http://www.baidu.com/"; CInternetSession::GetCookie(pszURL, "", strCookie); printf("%s\n", 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\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
//获取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; }
Keep it simple!