curl之cookie

 

curl如何处理cookie

curl的easy接口中提供了5个与cookie有关的option,其中,CURLOPT_COOKIEFILE,CURLOPT_COOKIEJAR,CURLOPT_COOKIELIST都会打开curl的cookie引擎,使得curl在收到http response时解析header field中的cookie。

设置 CURLOPT_COOKIEFILE 会使curl下一次发请求时从指定的文件中读取cookie。 
设置 CURLOPT_COOKIEJAR 会使curl在调用  curl_easy_cleanup的时候把cookie保存到指定的文件中。
设置CURLOPT_COOKIELIST会把指定的cookie字符串列表加入easy handle维护的cookie列表中。每个cookie字符串要么符合HTTP response header的"Set-Cookie: NAME=VALUE;..."格式,要么符合Netscape cookie格式。

CURLOPT_COOKIE用于设置一个分号分隔的“NAME=VALUE”列表,用于在HTTP request header中设置Cookie header。

curl内部使用Cookie和CookieInfo两个struct保存cookie信息。 为一个easy handle设置 CURLOPT_SHARE 选项,并且指定的share handle启用了cookie共享功能, 则easy handle会使用share handle中的共享cookie列表。
例如:

  1. int PostUrlchar* url, void *savepath)  
  2. {  
  3.     // 初始化libcurl  
  4.     CURLcode return_code;  
  5.     return_code = curl_global_init(CURL_GLOBAL_WIN32);  
  6.     if (CURLE_OK != return_code)  
  7.         return 0;  
  8.     CURL *curl_handle;  
  9.     CURLcode res;  
  10.     curl_handle = curl_easy_init();  
  11.     curl_easy_setopt(curl_handle, CURLOPT_URL, url);  
  12.     curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, true);  
  13.     curl_easy_setopt(curl_handle, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");   
  14.     curl_easy_setopt(curl_handle, CURLOPT_COOKIEJAR, "cookie.txt");   
  15.     curl_easy_setopt(curl_handle, CURLOPT_COOKIEFILE, "cookie.txt");   
  16.     curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_callback);  
  17.     curl_easy_setopt( curl_handle, CURLOPT_WRITEDATA, savepath );  
  18.     curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS,postLoginData);  
  19.     res = curl_easy_perform(curl_handle);   
  20.     curl_easy_cleanup(curl_handle);  
  21.     curl_global_cleanup();  
  22.     if (res == CURLE_OK)  
  23.     {  
  24.         return 1;  
  25.     }  
  26.     return 0;  
  27. }  
  28.   
  29. int GetUrl(string url,void *buffer)  
  30. {  
  31.     // 初始化libcurl  
  32.     CURLcode return_code;  
  33.     return_code = curl_global_init(CURL_GLOBAL_WIN32);  
  34.     if (CURLE_OK != return_code)  
  35.         return 0;  
  36.     CURL *easy_handle = curl_easy_init();  
  37.     if (NULL == easy_handle)  
  38.     {    
  39.         curl_global_cleanup();  
  40.         return 0;  
  41.     }  
  42.     // 设置easy handle属性  
  43.     curl_easy_setopt(easy_handle, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");   
  44.     curl_easy_setopt(easy_handle,CURLOPT_FOLLOWLOCATION,TRUE);   
  45.     curl_easy_setopt(easy_handle, CURLOPT_URL, url.c_str());  
  46.     curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_callback);  
  47.     curl_easy_setopt(easy_handle, CURLOPT_WRITEDATA, buffer);  
  48.     curl_easy_setopt(easy_handle, CURLOPT_COOKIEFILE,"cookie.txt");  
  49.     curl_easy_setopt(easy_handle, CURLOPT_COOKIEJAR, "cookie.txt");  
  50.     curl_easy_perform(easy_handle);   
  51.     curl_easy_cleanup(easy_handle);  
  52.     curl_global_cleanup();  
  53.     return 1;  
  54. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值