libcurl之cookie操作

news/2024/5/18 13:28:51 标签: curl, cookie, c语言

写在最前

C编程需要网页请求时当然首选libcurl库啦,涉及到登录的肯定需要对cookie操作了。所以本文主要是记录一下接收和发送cookie的方法,以及需要注意的地方。

curl导入cookie的两个方法">1、发送(往curl导入)cookie的两个方法:

(1)CURLOPT_COOKIELIST
curl_easy_setopt(curl, CURLOPT_COOKIELIST, my_cookie);
这个操作不方便的地方在于要自己组织cookies字符串

(2)CURLOPT_COOKIEFILE
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, “cookies.txt”);
cookie.txt中的cookies是由下面说到的CURLOPT_COOKIEJAR操作得到的。

PS:两个方法在导入时间上还有一点不同之处:
CURLOPT_COOKIELIST:my_cookie is imported immediately via CURLOPT_COOKIELIST.
CURLOPT_COOKIEFILE:The list of cookies in cookies.txt will not be imported until right before a transfer is performed.
更多不同详见官网说明

curl导出cookie的方法">2、获取(从curl导出)cookie的方法:

curl_easy_setopt(curl, CURLOPT_COOKIEJAR, “cookies.txt”);
所以CURLOPT_COOKIEJAR与CURLOPT_COOKIEFILE配合使用还是很方便的。

注意
导出操作(CURLOPT_COOKIEJAR)是在调用curl_easy_cleanup之后。官网原话"Cookies are exported after curl_easy_cleanup is called."
这其实没什么,但就被我撞上了。昨天删除部分打印的时候不小心(dd操作变成d+方向键下,vim党)把·curl_easy_cleanup·删除了,结果发现在下次请求时一直返回失败,最后发现cookies.txt文件不存在导致请求没有带上cookies,这才发现问题所在(大大一个囧)

以下是官网的EXAMPLE:

/* This example shows an inline import of a cookie in Netscape format.
You can set the cookie as HttpOnly to prevent XSS attacks by prepending
#HttpOnly_ to the hostname. That may be useful if the cookie will later
be imported by a browser.
*/

#define SEP  "\t"  /* Tab separates the fields */

char *my_cookie =
  "example.com"    /* Hostname */
  SEP "FALSE"      /* Include subdomains */
  SEP "/"          /* Path */
  SEP "FALSE"      /* Secure */
  SEP "0"          /* Expiry in epoch time format. 0 == Session */
  SEP "foo"        /* Name */
  SEP "bar";       /* Value */

/* my_cookie is imported immediately via CURLOPT_COOKIELIST.
*/
curl_easy_setopt(curl, CURLOPT_COOKIELIST, my_cookie);

/* The list of cookies in cookies.txt will not be imported until right
before a transfer is performed. Cookies in the list that have the same
hostname, path and name as in my_cookie are skipped. That is because
libcurl has already imported my_cookie and it's considered a "live"
cookie. A live cookie won't be replaced by one read from a file.
*/
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookies.txt");  /* import */

/* Cookies are exported after curl_easy_cleanup is called. The server
may have added, deleted or modified cookies by then. The cookies that
were skipped on import are not exported.
*/
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt");  /* export */

curl_easy_perform(curl);  /* cookies imported from cookies.txt */

curl_easy_cleanup(curl);  /* cookies exported to cookies.txt */

from: https://curl.haxx.se/libcurl/c/CURLOPT_COOKIELIST.html


http://www.niftyadmin.cn/n/790910.html

相关文章

网络字节序与主机字节序的转换

前言 端口号和IP地址都是以网络字节序存储的,不是主机字节序。网络字节序都是大端模式,而我们常用的机器都是小端模式。要把主机字节序和网络字节序相互对应起来,需要对这两个字节存储优先顺序进行相互转化。其实这个转换实质是:…

今日头条 适配 终极版本

https://www.jianshu.com/p/4aa23d69d481

GoWithTheFlow

GoWithTheFlow http://notes.jetienne.com/2011/07/17/gowiththeflow.js-async-flow-control-with-a-zen-touch.html https://github.com/jeromeetienne/gowiththeflow.js GoWithTheFlow.js是一个异步的流控制库,可运行在node.js和浏览器中。它可以控制如何执行异步…

sparse image

前言 在使用make_ext4fs的时候有一个参数为-s,意思是sparse。使用这个参数制作出来的镜像就是sparse image。当时不明白是什么意思,所以查找了一下并记录下来。 简单地来说,sparse image是一种需要多少空间就分配多少的镜像(虚…

当你喜刷刷时,你可知为何朋友圈能这么流畅?

本文转载自科技猎 刚刚过去的国庆朋友圈摄影大赛,各种花式秀,热闹非凡。 朋友圈已让人欲罢不能。有人说,早上睁眼第一件事情,是刷朋友圈;晚上睡觉最后一件事情,还是刷朋友圈。 这背后是海量的信息数据和查看…

2325: [ZJOI2011]道馆之战

2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 813 Solved: 309[Submit][Status]Description 口袋妖怪(又名神奇宝贝或宠物小精灵)红/蓝/绿宝石中的水系道馆需要经过三个冰地才能到达馆主的面前,冰地中的每一个冰块都只能经过一次。当…

AES 加密 绝对好用

自己测过兼容6.0到9.0的AES 工具类采用AES/CBC/PKCS5Padding 绝对好用的哦 调用方式 String enText AesUtils2.encrypt(AesUtils2.KEY, para); // String enText AESUtil2.encrypt(para,AES_KEY);Toast.makeText(context, " 参数:" para "加密后…