gpt4 book ai didi

http - 在两个 curl 请求之间保存 cookie

转载 作者:可可西里 更新时间:2023-11-01 15:04:02 32 4
gpt4 key购买 nike

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

curl --head www.google.com

而且我知道我可以使用以下方法向我的请求添加 header

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

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

How can I efficiently preserve cookies between two cURL requests?

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

最佳答案

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

-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

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

curl_easy_getinfo() 的选项:

curl_easy_setopt() 的选项:

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

关于http - 在两个 curl 请求之间保存 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30760213/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com