[译]Cookies Without Chocolate Chips
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信息会在适当的时候发送出去。
[译]Cookies Without Chocolate Chips的更多相关文章
- linux c libcurl的简单使用(转)
curl是Linux下一个非常著名的下载库,通过这个库,可以很简单的实现文件的下载等操作.看一个简单的例子: #include <curl/curl.h> #include <std ...
- curl 基本使用简介
curl是Linux下一个非常著名的下载库,通过这个库,可以很简单的实现文件的下载等操作.看一个简单的例子: #include <curl/curl.h> #include <std ...
- Libcurl细说
libcurl教程 原文地址:http://curl.haxx.se/libcurl/c/libcurl-tutorial.html 译者:JGood(http://blog.csdn.net/J ...
- JAVA经典总结
Java经典实例(第二版) 1. 获取环境变量 Java代码 1. System.getenv("PATH"); 2. System.getenv("JAVA_HOME& ...
- 2016 Google code jam 答案
二,RoundC import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundE ...
- Brownie Slicing(二分枚举答案)
描述 Bessie has baked a rectangular brownie that can be thought of as an RxC grid (1 <= R <= 500 ...
- Flutter Widget框架概述
Flutter Widget采用现代响应式框架构建,这是从 React 中获得的灵感,中心思想是用widget构建你的UI. Widget描述了他们的视图在给定其当前配置和状态时应该看起来像什么.当w ...
- Hamcrest使用
What is Hamcrest? 什么是Hamcrest? Hamcrest is a library of matchers, which can be combined in to crea ...
- Hamcrest Tutorial
Java Hamcrest Home Hamcrest Tutorial Introduction Hamcrest is a framework for writing matcher object ...
随机推荐
- linux下crontab安装和使用(定时任务)
在Unix和Linux的操作系统之中,cron可以让系统在指定的时间,去执行某个指定的任务,crontab命令常用于管理对应的cron. 一.crontab在线安装 yum -y install vi ...
- Servlet的会话(Session)跟踪
以下内容引用自http://wiki.jikexueyuan.com/project/servlet/session-tracking.html: HTTP是一种“无状态”协议,这意味着每次客户端检索 ...
- 使用go语言实现简单的反向代理工具激活IntelliJ和PyCharm,持续更新
最近Jetbrians系列IDE更新至2017.3版本,激活检测机制也变成了动态封禁域名,导致大部分域名激活被屏蔽了,所以找了下资料,根据ilanyu的代码,改了下地址,实现了本地反向代理激活服务器. ...
- swift学习笔记(四)关于类的继承
在swift中,继承是区分类与其它对象的基本特征 继承后的子类能够重写父类的方法,包含类方法和实例方法,属性和附属脚本(subscript) 在继承过程中,构造器方法init()是不被继承的,须要显示 ...
- Xcode中git的用法介绍与"Please tell me who you are"问题的解决方式
我在之前多篇博客中解说了怎样使用命令行操作git,能够大大提高我们的工作效率.详细能够參考<Git学习札记><Git学习札记--进阶>等文章.事实上对于同一个工具,我们有不同的 ...
- 简单区分iphone和ipad的宏定义
在公共头文件里作例如以下定义: #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 使用时: if( IS_I ...
- PB MD5
适用环境:powerbuilder 10.0以后的版本号 window server2003以后的測试可用 PB也能够调用系统自带的DLL 实现MD5 当中md5file对大附件的处理速度也比第三方 ...
- Azure Pack演示样例缩放部署架构
- qt-qml移动开发之在ios上开发和部署app流程简单介绍
qt5.3已经全面支持移动开发,除了mac,windows,linux.还支持ios,android,wp,meego等移动平台,本教程是作者依据自己的经验,从头讲怎么样在ios上公布自己的app.因 ...
- karaf增加自己定义log4j的配置
配置文件: karaf_home/etc/org.ops4j.pax.logging.cfg 增加配置: ### direct log messages to stdout ### log4j.app ...