在两个curl请求之间保存cookie [英] Save cookies between two curl requests
问题描述
我知道使用 cURL
我可以使用
curl --head www.google.com
我知道我可以添加使用
curl --cookieKey = Valuewww.google.com
我目前正在测试一个需要持久cookie的问题,并且可能会有很多问题。
如何在两个
cURL
请求之间有效保存Cookie
blockquote>
如果可能的话使用临时文件进行存储。
解决方案code> --Cookie-jar 或
--dump-header
参数将接收到的cookie保存到文件中。--cookies
参数可以稍后从该文件中读取cookie。
-b,--cookie< name = data>
(HTTP)将数据作为cookie传递给HTTP服务器。它应该是之前从服务器收到的Set-Cookie:行中的数据。数据应采用格式NAME1 = VALUE1; NAME2 = VALUE2。
如果在该行中没有使用'='符号,它将被视为文件名,用于从中读取以前存储的cookie行,如果它们匹配,应该在本次会议中使用它们。使用这种方法还会激活cookie引擎,它将使curl记录传入的cookie,如果您将这与-l,--location选项结合使用,可能会很方便。从中读取cookie的文件的文件格式应该是纯HTTP头(Set-Cookie样式)或Netscape / Mozilla的Cookie文件格式。
使用-b,--cookie指定的文件仅用作输入。没有cookie会写入文件。 要存储cookie,请使用-c,--cookie-jar选项。
如果您使用此选项并且可能发生多次转帐,请谨慎行事。如果您使用NAME1 = VALUE1;格式或文件中使用Set-Cookie格式并且不指定域,那么将为任何域发送cookie(即使在遵循重定向之后)并且不能由服务器设置的cookie修改。如果cookie引擎已启用,并且服务器设置了相同名称的cookie,则两者都将在未来传输到该服务器时发送,可能不是您想要的。为了解决这些问题,在Set-Cookie中设置一个域(这样做将包括子域)或使用Netscape格式。
如果多次使用此选项,则会使用最后一个。
-c, - cookie-jar<文件名称>
(HTTP)指定要在完成的操作后将curl写入哪个文件。 Curl写入先前从指定文件中读取的所有Cookie以及从远程服务器接收的所有Cookie 。如果不知道cookie,则不会写入数据。该文件将使用Netscape cookie文件格式编写。如果您将文件名设置为单个短划线 - ,则Cookie将写入标准输出。
这个命令行选项将激活使曲线记录和使用cookie的cookie引擎。另一种激活它的方法是使用-b,--cookie选项。
如果无法创建或写入cookie jar,则整个curl操作不会失败,甚至无法清楚地报告错误。使用-v会显示一个警告,但这是您可能得到的关于这种可能致命情况的唯一可见反馈。
由于7.43.0以Set-Cookie格式导入的没有域名的cookie不会通过此选项导出。
如果多次使用此选项,将使用最后指定的文件名。
-D, - dump-header< file>
将协议头文件写入指定的文件。
当您想存储HTTP站点发送给您的标头时,此选项非常方便。然后可以通过使用-b,--cookie选项在第二次卷曲调用中读取标头中的Cookie! -c,--cookie-jar选项是存储Cookie的更好方式。
在FTP中使用时,FTP服务器响应行被认为是标题,因此被保存在那里。
如果多次使用此选项,最后一个将被使用
或者,不要使用命令行 cURL应用,编写一些使用 libCurl库。这会让你更直接地控制cookie处理。 libCurl有几个与HTTP cookies相关的功能:
的选项:
- CURLINFO_COOKIELIST - 获取所有已知的cookies
的选项curl_easy_setopt()
:
CURLOPT_COOKIE - 设置HTTP Cookie标头的内容
- 读取cookie的文件名
CURLOPT_COOKIEJAR - 存储cookies的文件名称
CURLOPT_COOKIESESSION - 开始一个新的cookie会话
CURLOPT_COOKIELIST - 添加或操作内存中的cookies
然后,您可以存储所需的cookies,然后分配它们根据需要以后的HTTP会话。
I know that using
cURL
I can see my received cookies / headers by usingcurl --head www.google.com
And I know that I can add headers to my request using
curl --cookie "Key=Value" www.google.com
I am currently working on testing an issue which requires persistent cookies, and there can be a lot of them.
How can I efficiently preserve cookies between two
cURL
requests?If possible using a temporary file for storage.
解决方案Use the
--cookie-jar
or--dump-header
parameter to save received cookies to a file. The--cookie
parameter can read back the cookies from that file later.-b, --cookie <name=data>
(HTTP) Pass the data to the HTTP server as a cookie. It is supposedly the data previously received from the server in a "Set-Cookie:" line. The data should be in the format "NAME1=VALUE1; NAME2=VALUE2".
If no '=' symbol is used in the line, it is treated as a filename to use to read previously stored cookie lines from, which should be used in this session if they match. Using this method also activates the cookie engine which will make curl record incoming cookies too, which may be handy if you're using this in combination with the -L, --location option. The file format of the file to read cookies from should be plain HTTP headers (Set-Cookie style) or the Netscape/Mozilla cookie file format.
The file specified with -b, --cookie is only used as input. No cookies will be written to the file. To store cookies, use the -c, --cookie-jar option.
Exercise caution if you are using this option and multiple transfers may occur. If you use the NAME1=VALUE1; format, or in a file use the Set-Cookie format and don't specify a domain, then the cookie is sent for any domain (even after redirects are followed) and cannot be modified by a server-set cookie. If the cookie engine is enabled and a server sets a cookie of the same name then both will be sent on a future transfer to that server, likely not what you intended. To address these issues set a domain in Set-Cookie (doing that will include sub-domains) or use the Netscape format.
If this option is used several times, the last one will be used.
-c, --cookie-jar <file name>
(HTTP) Specify to which file you want curl to write all cookies after a completed operation. Curl writes all cookies previously read from a specified file as well as all cookies received from remote server(s). If no cookies are known, no data will be written. The file will be written using the Netscape cookie file format. If you set the file name to a single dash, "-", the cookies will be written to stdout.
This command line option will activate the cookie engine that makes curl record and use cookies. Another way to activate it is to use the -b, --cookie option.
If the cookie jar can't be created or written to, the whole curl operation won't fail or even report an error clearly. Using -v will get a warning displayed, but that is the only visible feedback you get about this possibly lethal situation.
Since 7.43.0 cookies that were imported in the Set-Cookie format without a domain name are not exported by this option.
If this option is used several times, the last specified file name will be used.
-D, --dump-header <file>
Write the protocol headers to the specified file.
This option is handy to use when you want to store the headers that an HTTP site sends to you. Cookies from the headers could then be read in a second curl invocation by using the -b, --cookie option! The -c, --cookie-jar option is a better way to store cookies.
When used in FTP, the FTP server response lines are considered being "headers" and thus are saved there.
If this option is used several times, the last one will be used
Alternatively, instead of using the command-line cURL app, write some code that uses the libCurl library. That will give you more direct control over cookie handling. libCurl has several features related to HTTP cookies:
Options for
curl_easy_getinfo()
:
- CURLINFO_COOKIELIST - get all known cookies
Options for
curl_easy_setopt()
:
CURLOPT_COOKIE - set contents of HTTP Cookie header
CURLOPT_COOKIEFILE - file name to read cookies from
CURLOPT_COOKIEJAR - file name to store cookies to
CURLOPT_COOKIESESSION - start a new cookie session
CURLOPT_COOKIELIST - add to or manipulate cookies held in memory
Then you can store the cookies however you want, and assign them as needed to later HTTP sessions.
这篇关于在两个curl请求之间保存cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!