使用curl保存和显示cookie

v7pvogib  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(239)

对于一些项目,我需要在发送curl请求时激活cookie,并在稍后的另一个调用中使用这些cookie。举个例子,假设我想调用https://www.heise.de并将接收到的cookie保存在那里。当我heise.de用浏览器打开www.example.com并查看我的cookie时,它是这样的

我假设这些是Cookie,这是在连接到网站时生成的。
对于测试,我现在想对curl做同样的事情,我希望得到类似的结果。
在Ubuntu控制台中,我尝试了以下操作

curl -L -c cookie.txt -b cookie.txt www.heise.de

当我打开文件时,我只看到:

# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

它生成了文件,但我不明白,为什么没有cookie,我一定是漏掉了什么,比如我在图像中看到的东西,不应该在那里。
如果能提供一些帮助,我将不胜感激:)

avwztpqn

avwztpqn1#

这是令人沮丧的。
我尝试了我书中所有的技巧,这些技巧在过去都有助于得到回应。
在我看来,这个网站不希望你和你的 curl 或wget。
并且它们非常擅长检测curl请求。
问题是当用curl发出请求时,这个URL不会在头中返回cookie。对于我尝试过的一些请求头,根本没有响应。
下面是 curl https://www.heise.de/时的响应标头

HTTP/2 200 
server: nginx 
date: Tue, 08 Nov 2022 22:58:40 GMT 
content-type: text/html; charset=UTF-8 
last-modified: Tue, 08 Nov 2022 22:58:40 GMT 
age: 18 
accept-ranges: bytes 
strict-transport-security: max-age=15768000 
x-frame-options: DENY 
x-xss-protection: 1; mode=block 
x-content-type-options: nosniff 
x-hacc-refreshed: 
vary: Accept-Encoding, X-Export-Format, X-Export-Agent, X-Export-IAP 
cache-control: no-store 
content-length: 756164

我的想法:

wget -SO- -T 5 -t 1 https://www.heise.de/ > test.html

回应:

--2022-11-08 18:46:53--  https://www.heise.de/
Resolving www.heise.de (www.heise.de)... 2a02:2e0:3fe:1001:7777:772e:2:85, 193.99.144.85
Connecting to www.heise.de (www.heise.de)|2a02:2e0:3fe:1001:7777:772e:2:85|:443... connected.
HTTP request sent, awaiting response...
  HTTP/1.1 200 OK
  Server: nginx
  Date: Tue, 08 Nov 2022 23:46:38 GMT
  Content-Type: text/html; charset=UTF-8
  Last-Modified: Tue, 08 Nov 2022 23:46:38 GMT
  Age: 16
  Accept-Ranges: bytes
  Strict-Transport-Security: max-age=15768000
  x-frame-options: DENY
  X-XSS-Protection: 1; mode=block
  X-Content-Type-Options: nosniff
  X-Hacc-Refreshed:
  Vary: Accept-Encoding, X-Export-Format, X-Export-Agent, X-Export-IAP
  Cache-Control: no-store
  Content-Length: 757423
  Connection: keep-alive
Length: 757423 (740K) [text/html]

相关问题