在两个 curl 请求之间保存 cookie [英] Save cookies between two curl requests

查看:61
本文介绍了在两个 curl 请求之间保存 cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道使用 cURL 我可以通过使用看到我收到的 cookie/标头

I know that using cURL I can see my received cookies / headers by using

curl --head www.google.com

而且我知道我可以使用

curl --cookie "Key=Value" www.google.com

我目前正在测试一个需要持久性 cookie 的问题,而且可能有很多这样的问题.

I am currently working on testing an issue which requires persistent cookies, and there can be a lot of them.

如何在两个 cURL 请求之间有效地保留 cookie?

How can I efficiently preserve cookies between two cURL requests?

如果可能,使用临时文件进行存储.

If possible using a temporary file for storage.

推荐答案

使用 --cookie-jar--dump-header 参数将接收到的 cookie 保存到一份文件.--cookie 参数可以稍后从该文件中读回 cookie.

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

-b, --cookie <name=data>

(HTTP) 将数据作为 cookie 传递给 HTTP 服务器.它应该是先前在Set-Cookie:"行中从服务器接收到的数据.数据的格式应为NAME1=VALUE1;NAME2=VALUE2".

(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".

如果行中未使用="符号,则将其视为文件名,用于从中读取先前存储的 cookie 行,如果匹配,则应在此会话中使用.使用此方法还会激活 cookie 引擎,该引擎也将使 curl 记录传入的 cookie,如果您将它与 -L、--location 选项结合使用,这可能会很方便.要从中读取 cookie 的文件的文件格式应该是纯 HTTP 标头(Set-Cookie 样式)或 Netscape/Mozilla cookie 文件格式.

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.

-b、--cookie 指定的文件仅用作输入.不会将 cookie 写入文件.要存储 cookie,请使用 -c、--cookie-jar 选项.

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.

如果您使用此选项,请谨慎操作,可能会发生多次传输.如果你使用 NAME1=VALUE1;格式,或者在文件中使用 Set-Cookie 格式并且不指定域,那么 cookie 将发送到任何域(即使在重定向之后)并且不能被服务器设置的 cookie 修改.如果启用了 cookie 引擎并且服务器设置了一个同名的 cookie,那么两者都将在以后的传输中发送到该服务器,这可能不是您想要的.为了解决这些问题,在 Set-Cookie 中设置一个域(这样做将包括子域)或使用 Netscape 格式.

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 <文件名>

-c, --cookie-jar <file name>

(HTTP) 指定您希望 curl 在完成操作后将所有 cookie 写入哪个文件.Curl 写入之前从指定文件读取的所有 cookie 以及从远程服务器接收的所有 cookie.如果不知道 cookie,则不会写入任何数据.该文件将使用 Netscape cookie 文件格式写入.如果您将文件名设置为单个破折号-",则 cookie 将写入标准输出.

(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.

此命令行选项将激活进行 curl 记录和使用 cookie 的 cookie 引擎.另一种激活它的方法是使用 -b, --cookie 选项.

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.

如果无法创建或写入cookie jar,则整个curl操作不会失败,甚至不会明确报告错误.使用 -v 将显示警告,但这是您获得的有关这种可能致命情况的唯一可见反馈.

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.

从 7.43.0 开始,以 Set-Cookie 格式导入的 cookie 不通过此选项导出.

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 <文件>

-D, --dump-header <file>

将协议头写入指定文件.

Write the protocol headers to the specified file.

当您想要存储 HTTP 站点发送给您的标头时,使用此选项非常方便.然后可以使用 -b, --cookie 选项在第二次 curl 调用中读取来自标题的 cookie!-c, --cookie-jar 选项是存储 cookie 的更好方法.

在 FTP 中使用时,FTP 服务器响应行被视为标题",因此保存在那里.

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

或者,不使用命令行cURL 应用,而是编写一些使用libCurl 库.这将使您更直接地控制 cookie 处理.libCurl 有几个与 HTTP cookie 相关的特性:

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:

curl_easy_getinfo() 的选项:

  • CURLINFO_COOKIELIST - get all known cookies

curl_easy_setopt() 的选项:

  • CURLOPT_COOKIE - set contents of HTTP Cookie header

CURLOPT_COOKIEFILE - 从中​​读取 cookie 的文件名

CURLOPT_COOKIEFILE - file name to read cookies from

CURLOPT_COOKIEJAR - 用于存储 cookie 的文件名

CURLOPT_COOKIEJAR - file name to store cookies to

CURLOPT_COOKIESESSION - 开始新的 cookie 会话

CURLOPT_COOKIESESSION - start a new cookie session

CURLOPT_COOKIELIST - 添加或操作保存在内存中的 cookie

CURLOPT_COOKIELIST - add to or manipulate cookies held in memory

然后您可以根据需要存储 cookie,并根据需要将它们分配给以后的 HTTP 会话.

Then you can store the cookies however you want, and assign them as needed to later HTTP sessions.

这篇关于在两个 curl 请求之间保存 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆