是否可以使用CURL设置Cookie内容? [英] Is it possible to set the cookie content with CURL?

查看:945
本文介绍了是否可以使用CURL设置Cookie内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种方法来指定CURL的Cookie数据。我已经找到一些解决方案如何保存来自访问页面的Cookie,但这不是我需要的。

解决方案

你真的应该阅读文档 - 其中列出了完全符合您期望的关键字,并包含大量有用的信息:



曲奇饼。它应该是
以前在Set-Cookie:行中从服务器接收的数据。
数据的格式应为NAME1 = VALUE1; NAME2 = VALUE2。



如果行中没有使用'='符号,处理为文件名
用于读取以前存储的cookie行,如果它们匹配,则应在此会话中使用
。使用这种方法还激活
cookie解析器,这将使curl记录传入的cookie,
如果你使用这个结合-L,
- 位置选项。要读取Cookie的文件的文件格式应为纯HTTP头或Netscape / Mozilla cookie文件
格式。



注意, b,--cookie仅用作输入。
没有cookie将存储在文件中。要存储cookie,使用-c,
--cookie-jar选项,或者甚至可以使用-D,--dump-header!将HTTP头保存到文件中。



如果此选项设置多次,最后一个将是使用的



I have been searching for a way, to specify the cookie data for CURL. I have found some solutions on how to save the cookies from a visited page, but that's not what I need. What I want is, to write the data for the cookie myself, so CURL uses it.

解决方案

You really should read the documentation - it's listed with exactly the keywords you'd expect and contains a lot of helpful info:

-b, --cookie

(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 parser" 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 or the Netscape/Mozilla cookie file format.

NOTE that the file specified with -b, --cookie is only used as input. No cookies will be stored in the file. To store cookies, use the -c, --cookie-jar option or you could even save the HTTP headers to a file using -D, --dump-header!

If this option is set more than once, the last one will be the one that's used.

这篇关于是否可以使用CURL设置Cookie内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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