使用curl发送cookie

发布于 2024-12-01 12:27:43 字数 224 浏览 0 评论 0原文

我正在使用curl来检索cookie,如下所示:

curl -c cookies.txt url

然后我从cookies.txt文件中解析我想要的cookie并使用cookie再次发送请求

curl -b "name=value" url 

这是发送cookie的正确方法吗? 有没有更简单的方法?

I am using curl to retrieve cookies like so:

curl -c cookies.txt url

then I parse the cookie I want from the cookies.txt file and send the request again with the cookie

curl -b "name=value" url 

Is this the correct way to send the cookie?
Is there a simpler way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷夜 2024-12-08 12:27:43

您还可以使用 -b 指定从中读取 cookie 的 cookie 文件。

在许多情况下使用 -c-b 到同一个文件就是你想要的:

curl -b cookies.txt -c cookies.txt http://example.com

进一步

仅使用 -c 将使curl启动没有cookies但仍然解析和理解cookies并且如果使用重定向或多个 URL,它将在单次调用中使用收到的 cookie,然后最终将它们全部写入输出文件。

-b 选项将一组初始 cookie 提供给curl,以便它在启动时了解它们,并激活curl 的 cookie 解析器,以便它也解析和使用传入的 cookie。

另请参阅

Everything curl< 中的 cookie 章节 /em> 书。

You can also use -b to specify a cookie file from which to read the cookies.

In many situations using -c and -b to the same file is what you want:

curl -b cookies.txt -c cookies.txt http://example.com

Further

Using only -c will make curl start with no cookies but still parse and understand cookies and if redirects or multiple URLs are used, it will then use the received cookies within the single invoke before it writes them all to the output file in the end.

The -b option feeds a set of initial cookies into curl so that it knows about them at start, and it activates curl's cookie parser so that it'll parse and use incoming cookies as well.

See Also

The cookies chapter in the Everything curl book.

滴情不沾 2024-12-08 12:27:43

.example.com TRUE / FALSE 1560211200 MY_VARIABLE MY_VALUE

cookies 文件格式显然由每个 cookie 一行组成,每行由以下七个制表符分隔字段组成:

  • 域 - 创建的域并且可以读取变量。
  • flag - TRUE/FALSE 值,指示给定域内的所有计算机是否可以访问该变量。该值由浏览器自动设置,具体取决于您为域设置的值。
  • path - 变量有效的域内的路径。
  • secure - TRUE/FALSE 值,指示是否需要与域的安全连接来访问变量。
  • 过期 - 变量过期的 UNIX 时间。 UNIX 时间定义为自 1970 年 1 月 1 日 00:00:00 GMT 以来的秒数。
  • 名称 - 变量的名称。
  • 值 - 变量的值。

来自 http://www.cookiecentral.com/faq/#3.5

.example.com TRUE / FALSE 1560211200 MY_VARIABLE MY_VALUE

The cookies file format apparently consists of a line per cookie and each line consists of the following seven tab-delimited fields:

  • domain - The domain that created AND that can read the variable.
  • flag - A TRUE/FALSE value indicating if all machines within a given domain can access the variable. This value is set automatically by the browser, depending on the value you set for domain.
  • path - The path within the domain that the variable is valid for.
  • secure - A TRUE/FALSE value indicating if a secure connection with the domain is needed to access the variable.
  • expiration - The UNIX time that the variable will expire on. UNIX time is defined as the number of seconds since Jan 1, 1970 00:00:00 GMT.
  • name - The name of the variable.
  • value - The value of the variable.

From http://www.cookiecentral.com/faq/#3.5

时光是把杀猪刀 2024-12-08 12:27:43

如果您在 Firefox 上安装了 Firebug,只需打开该 url。在网络面板中,右键单击并选择复制为 cURL。您可以看到此 Web 调用的所有curl 参数。

if you have Firebug installed on Firefox, just open the url. In the network panel, right-click and select Copy as cURL. You can see all curl parameters for this web call.

陈独秀 2024-12-08 12:27:43

很烦人,官网没有cookie文件exmpale https://ec.haxx.se/http /http-cookies

最后,我发现它不起作用,如果你的文件内容只是像这样复制,

foo1=bar;foo2=bar2

我猜格式必须看起来像 @Agustí Sánchez 所说的样式
。您可以通过-c在网站上创建cookie文件来测试它。

所以尝试这种方式,它有效

curl -H "Cookie:`cat ./my.cookie`"   http://xxxx.com

你可以从 chrome 控制台网络选项卡复制 cookie。

Very annoying, no cookie file exmpale on the official website https://ec.haxx.se/http/http-cookies.

Finnaly, I find it does not work, if your file content is just copyied like this

foo1=bar;foo2=bar2

I gusess the format must looks the style said by @Agustí Sánchez
. You can test it by -c to create a cookie file on a website.

So try this way, it works

curl -H "Cookie:`cat ./my.cookie`"   http://xxxx.com

You can just copy the cookie from chrome console network tab.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文