VC++设置与获取cookies的几种方法

 

VC++设置与获取cookies的几种方法  

2011-06-11 18:05:19|  分类: 默认分类 |  标签: |字号 订阅

最简单的获取cookies的是用InternetGetCookie,函数原型如下:

BOOL InternetGetCookie(
LPCTSTR lpszUrl,
LPCTSTR lpszCookieName,
LPSTR lpCookieData,
LPDWORD lpdwSize
);

这样几句就可以了

char Cookie[300];
char *sURL = "http://host286.com/articles.asp?id=653";
InternetGetCookie(sURL,NULL,Cookie,&leng);

缺陷是nternetGetCookie 只读取 COOKIES目录下生成的COOKIE,遇到HTTPONLY属性则不生成本地COOKIES中的文件,而是直接通过HTTP头来传输。

这时可以用下列代码

char * pszURL = "http://host286.com/";
BOOL bRes = CInternetSession::GetCooke(pszURL,"",strCookie);

DWORD buflen = 300;
char strCookie[300] = {0};
CString strCookie;
fileGet->QueryInfo(HTTP_QUERY_SET_COOKIE,strCookie,&buflen,NULL);

或者

CString strCookie;
     char * pszURL = "http://host286.com/";
     BOOL bRes = CInternetSession::GetCookie(pszURL,"",strCookie);

设置cookies的方法:
InternetSetCookie("http://host286.com",NULL,"TestData=Test;expires=Sat,01-Jan-2012 00:00:00GMT");

Microsoft 参考资料:

Managing   Cookies

--------------------------------------------------------------------------------

Under   http   protocol,   a   server   or   a   script   uses   cookies   to   maintain   state   information   on   the   client   workstation.   The   Microsoft?   Win32?   Internet   functions   have   implemented   a   persistent   cookie   database   for   this   purpose.   The   Win32   Internet   cookie   functions   are   used   to   set   cookies   in   and   access   cookies   from   the   cookie   database.   For   more   information   on   http   cookies,   see   HTTP   Cookies.

The   Win32   Internet   functions   InternetSetCookie   and   InternetGetCookie   can   be   used   to   manage   cookies.  

Note     The   implementation   of   these   functions   is   evolving.
Using   Cookie   Functions
The   following   functions   allow   an   application   to   create   or   retrieve   cookies   in   the   cookie   database.

InternetGetCookie   Retrieves   cookies   for   the   specified   URL   and   all   its   parent   URLs.  
InternetSetCookie   Sets   a   cookie   on   the   specified   URL.  

Unlike   most   of   the   Win32   Internet   functions,   the   cookie   functions   do   not   require   a   call   to   InternetOpen.   Cookies   that   have   an   expiration   date   are   stored   in   the   windows\cookies   directory.   Cookies   that   don 't   have   an   expiration   date   are   stored   in   memory   and   are   available   only   to   the   process   in   which   they   were   created.

Getting   a   Cookie
InternetGetCookie   returns   the   cookies   for   the   specified   URL   and   all   its   parent   URLs.

The   following   example   demonstrates   a   call   to   InternetGetCookie.  

Show   Example

char   szURL[256];                     //   buffer   to   hold   the   URL
LPSTR   lpszData   =   NULL;         //   buffer   to   hold   the   cookie   data
DWORD   dwSize=0;                       //   variable   to   get   the   buffer   size   needed

//   Insert   code   to   retrieve   the   URL.

retry:

//   The   first   call   to   InternetGetCookie   will   get   the   required  
//   buffer   size   needed   to   download   the   cookie   data.
if   (!InternetGetCookie(szURL,   NULL,   lpszData,   &dwSize))
{
//   Check   for   an   insufficient   buffer   error.
        if   (GetLastError()==   ERROR_INSUFFICIENT_BUFFER)
        {
                //   Allocate   the   necessary   buffer.
                lpszData   =   new   char[dwSize];

                //   Try   the   call   again.
                goto   retry;
        }
else
        {
                //   Insert   error   handling   code.
        }

}
else
{
        //   Insert   code   to   display   the   cookie   data.

        //   Release   the   memory   allocated   for   the   buffer.
        delete[]lpszData;
}
Setting   a   Cookie
InternetSetCookie   is   used   to   set   a   cookie   on   the   specified   URL.   InternetSetCookie   can   create   both   persistent   and   session   cookies.

Persistent   cookies   are   cookies   that   have   an   expiration   date.   These   cookies   are   stored   in   the   Windows\System   directory.

Session   cookies   are   stored   in   memory   and   can   be   accessed   only   by   the   process   that   created   them.

The   data   for   the   cookie   should   be   in   the   format:

NAME=VALUE
For   the   expiration   date,   the   format   must   be:

DAY,   DD-MMM-YYYY   HH:MM:SS   GMT
DAY   is   the   three-letter   abbreviation   for   the   day   of   the   week,   DD   is   the   day   of   the   month,   MMM   is   the   three-letter   abbreviation   for   the   month,   YYYY   is   the   year,   and   HH:MM:SS   is   the   time   of   the   day   in   military   time.

The   following   example   demonstrates   two   calls   to   InternetSetCookie.   The   first   call   creates   a   session   cookie   and   the   second   creates   a   persistent   cookie.

BOOL   bReturn;

//   Create   a   session   cookie.
bReturn   =   InternetSetCookie( "http://www.adventure_works.com ",   NULL,
                        "TestData   =   Test ");

//   Create   a   persistent   cookie.
bReturn   =   InternetSetCookie( "http://www.adventure_works.com ",   NULL,
                          "TestData   =   Test;   expires   =   Sat,   01-Jan-2000   00:00:00   GMT ");
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值