Cookies Without Chocolate Chips
In the HTTP sense, a cookie is a name with an associated value. A server sends the name and value to the client, and expects it to get sent back on every subsequent request to the server that matches the particular conditions set. The conditions include that the domain name and path match and that the cookie hasn't become too old.
在Http中,cookie由name和value组成。
服务器发送name和value到客户端,并希望客户端每次发送子请求时附带上这些信息。
cookie包括 域名、路径、是否过期。
In real-world cases, servers send new cookies to replace existing ones to update them. Server use cookies to "track" users and to keep "sessions".
实际上,服务器发送新的cookie新到替换旧的信息。
服务器通过cookie追踪用户和保持session。
Cookies are sent from server to clients with the header Set-Cookie: and they're sent from clients to servers with the Cookie: header.
服务器发送cookie到客户端的时候,会发送http头:Set-Cookie
客户端发送cookie信息到服务器的时候,会发送Http头:
To just send whatever cookie you want to a server, you can use CURLOPT_COOKIE to set a cookie string like this:
可以通过设置CURLOPT_COOKIE属性,来发送cookie信息:
curl_easy_setopt(easyhandle, CURLOPT_COOKIE, "name1=var1; name2=var2;");
In many cases, that is not enough. You might want to dynamically save whatever cookies the remote server passes to you, and make sure those cookies are then used accordingly on later requests.
在大多数情况下,这无法满足需求。
你可以需要保存远程发送到本地的cookie信息,
并且在接下来的请求中附带cookie信息
One way to do this, is to save all headers you receive in a plain file and when you make a request, you tell libcurl to read the previous headers to figure out which cookies to use. Set the header file to read cookies from with CURLOPT_COOKIEFILE.
另外一种方法是,保存所有的收到的header到一个文件中,当需要发送请求时,让libcurl读取该文件,取出对应的cookie信息。
使用CURLOPT_COOKIEFILE设置libcurl读取的cookie的文件。
The CURLOPT_COOKIEFILE option also automatically enables the cookie parser in libcurl. Until the cookie parser is enabled, libcurl will not parse or understand incoming cookies and they will just be ignored. However, when the parser is enabled the cookies will be understood and the cookies will be kept in memory and used properly in subsequent requests when the same handle is used. Many times this is enough, and you may not have to save the cookies to disk at all. Note that the file you specify to ICURLOPT_COOKIEFILE(3) doesn't have to exist to enable the parser, so a common way to just enable the parser and not read any cookies is to use the name of a file you know doesn't exist.
并且CURLOPT_COOKIEFILE选项会使libcurl自动分析cookie。
除非cookie解析器被激活,否则libcurl不会分析cookie信息,而是全部忽略。
一旦cookie解析器被激活,cookie信息则会自动被保存在内存中,并且自动会在接下来对应的请求中使用
你完全可以不去保存cookie信息到磁盘中
需要注意,CURLOPT_COOKIEFILE(3)所指定的文件不一定要存在才能激活cookie分析功能,
所以一个激活cookie分析器常用的方法就是设置一个不存在的文件。
If you would rather use existing cookies that you've previously received with your Netscape or Mozilla browsers, you can make libcurl use that cookie file as input. The CURLOPT_COOKIEFILE is used for that too, as libcurl will automatically find out what kind of file it is and act accordingly.
你可以让libcurl使用一个之前用Netscape或者浏览器获取的cookie文件
也是用过CURLOPT_COOKIEFILE设置。
Perhaps the most advanced cookie operation libcurl offers, is saving the entire internal cookie state back into a Netscape/Mozilla formatted cookie file. We call that the cookie-jar. When you set a file name with CURLOPT_COOKIEJAR, that file name will be created and all received cookies will be stored in it when curl_easy_cleanup is called. This enables cookies to get passed on properly between multiple handles without any information getting lost.
也许libcurl在cookie方面提供最大的扩展是:保存整个内部的cookie信息到Netscape/Mozilla的格式。
我们把他称为cookie-jar
当你设置CURLOPT_COOKIEJAR属性设置一个文件时,
文件会被创建,并且在执行curl_easy_cleanup函数之后,所有接受到的cookie信息会被存储到该文件中。
这样使得所有的cookie信息会在适当的时候发送出去。