libcurl中cookie的使用

異步multi沒有使用,easy是最常用的。cookie我試了下面4個宏,基本全了。cookie的細節很多,但不是我的重點,就這麼籠統的寫一下。

CURLOPT_COOKIE

參考

https://curl.haxx.se/libcurl/c/CURLOPT_COOKIE.html

說明

1、第一種自定義cookie,只需調用一次,多次調用以最後一次爲準
2、格式必須是NAME=CONTENTS,例如:"name1=content1; name2=content2;"
3、和curl的cookie引擎無關,感覺挺low的

 

CURLOPT_COOKIELIST

參考

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

說明

1、也是設置發送的http請求中的cookie功能

2、啓動curl的cookie引擎,和CURLOPT_COOKIEFILE、CURLOPT_COOKIEJAR是一組

3、支持兩種格式:

#type 1
Set-Cookie: cookie1=val1;
#type 2
#this from https://curl.haxx.se/libcurl/c/CURLOPT_COOKIELIST.html
#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 */

 

CURLOPT_COOKIEFILE

參考

https://curl.haxx.se/libcurl/c/CURLOPT_COOKIEFILE.html

說明

1、從文件中讀取cookie,內容按標準的網景格式就行

#like this:
# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

example.com     FALSE   /       FALSE   0       foao    baar
example.com     FALSE   /       FALSE   0       foo     bar

2、還有“”和“-”這2種默認值,官網上有說明

 

CURLOPT_COOKIEJAR

參考

https://curl.haxx.se/libcurl/c/CURLOPT_COOKIEJAR.html

說明

1、將服務器發來的cookie保存成文件,上面那個就是調這個宏保存下來的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章