我正在尝试使用curl 登录pinterest.com。我得到以下请求-响应流程:
- GET-请求登录表单并抓取隐藏字段(csrftoken)
- POST-请求登录凭据(电子邮件和密码)和抓取的 csrf 令牌
- 接收登录会话 Cookie
使用 Curl,我可以看到发送和接收的以下标头:
GET /login/?next=%2F HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
Host: pinterest.com
Referer:
Accept: text/html,application/xhtml+xml,application/xml,*/*
Accept-Language: de-de,en-us
Connection: keep-alive
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Date: Tue, 10 Apr 2012 15:03:24 GMT
ETag: "45d6a85f0ede46f13f4fc751842ce5b7"
Server: nginx/0.8.54
Set-Cookie: csrftoken=dec6cb66064f318790c6d51e3f3a9612; Max-Age=31449600; Path=/
Set-Cookie: _pinterest_sess="eJyryMwNcTXOdtI3zXcKNq0qznIxyXVxK/KqSsy3tY8vycxNtfUN8a3yc3E09nXxLPdztLVVK04tLs5MsfXNAopVpVf6VnlW+Qba2gIAuqgZIg=="; Domain=pinterest.com; HttpOnly; expires=Tue, 17-Apr-2012 15:03:24 GMT; Max-Age=1334675004; Path=/
Vary: Cookie, Accept-Encoding
Content-Length: 4496
Connection: keep-alive
所以在步骤1之后,csrftoken和_pinterest_sess这两个cookie就被设置了。但是查看 cookiejar 文件(我使用 CURLOPT_COOKIEFILE 和 CURLOPT_COOKIEJAR 让curl 处理 cookie 处理)显示以下内容:
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.
pinterest.com FALSE / FALSE 1365519805 csrftoken dec6cb66064f318790c6d51e3f3a9612
#HttpOnly_.pinterest.com TRUE / FALSE -1626222087 _pinterest_sess "eJyryMwNcTXOdtI3zXcKNq0qznIxyXVxK/KqSsy3tY8vycxNtfUN8a3yc3E09nXxLPdztLVVK04tLs5MsfXNAopVpVf6VnlW+Qba2gIAuqgZIg=="
首先要注意的是 _pinterest_sess cookie 行前面的#HttpOnly_。我只是假设curl 可以很好地处理这个问题。但进一步看,我们可以看到负值被设置为到期日期:-1626222087
我不知道这是从哪里来的,因为 cookie 设置为“expires=Tue, 17-Apr-2012 15:03:24 GMT”(从今天算起,大约是未来 7 天)。
在下一个请求中,curl 将不会设置 _pinterest_sess cookie:
POST /login/?next=%2F HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
Host: pinterest.com
Referer: https://pinterest.com/login/?next=%2F
Cookie: csrftoken=dec6cb66064f318790c6d51e3f3a9612
Accept: text/html,application/xhtml+xml,application/xml,*/*
Accept-Language: de-de,en-us
Connection: keep-alive
Content-Length: 123
Content-Type: application/x-www-form-urlencoded
HTTP/1.1 302 FOUND
Content-Type: text/html; charset=utf-8
Date: Tue, 10 Apr 2012 15:05:26 GMT
ETag: "d41d8cd98f00b204e9800998ecf8427e"
Location: http://pinterest.com/
Server: nginx/0.8.54
Set-Cookie: _pinterest_sess="eJzLcssPCy4NTclIjvAOrjQzyywoCChISgvLDi+2tY9PrSjILEottvUN8a3yc4k09gtxrfRLt7VVK04tLs5MAYonV/qGeFb4ZkWW+4LES4tTi+KBEv4u6UZ+WYEmvlm+QOxZ6R/iWOEbEmgLAKNfJps="; Domain=pinterest.com; HttpOnly; expires=Tue, 17-Apr-2012 15:05:26 GMT; Max-Age=1334675126; Path=/
Vary: Cookie
Content-Length: 0
Connection: keep-alive
在响应中,由于curl 没有发送最后一个cookie,因此设置了另一个_pinterest_sess cookie。
目前,我不知道我是否做错了什么,或者curl是否无法正确解析cookie中的过期值。
任何帮助将不胜感激 :)
// 编辑
还有一件事:
根据http://opensource.apple.com/source/curl/curl-57/curl/lib/cookie.c函数curl_getdate()用于提取日期。有关该函数的文档列出了一些示例(http://curl.haxx.se/libcurl/c/curl_getdate.html):
- 1994 年 11 月 6 日,星期日 08:49:37 GMT
- 94 年 11 月 6 日星期日 08:49:37 GMT
- 1994 年 11 月 6 日星期日 08:49:37
- 1994 年 11 月 6 日 08:49:37 GMT
- 94 年 11 月 6 日 08:49:37 GMT
- 1994 年 11 月 6 日 08:49:37
- 1994 年 11 月 6 日 08:49:37
- 94 年 11 月 6 日 08:49:37
- 1994 年 11 月 6 日 08:49:37 GMT
- 2094 年 11 月 6 日 08:49:37
- 星期日 94 11 月 6 日 08:49:37
- 1994年11月6日
- 94 年 11 月 6 日
- 94 年 11 月 6 日星期日
- 1994年11月6日
- 周日/11 月/6/94/GMT
- 欧洲中部时间 1994 年 11 月 6 日,星期日 08:49:37
- 1994 年 11 月 6 日 08:49:37 美国东部时间
- 2004 年 9 月 12 日,星期日 15:05:58 -0700
- 2004 年 9 月 11 日,星期六 21:32:11 +0200
- 20040912 15:05:58 -0700
- 20040911 +0200
它们都不符合上述到期日期“Tue, 17-Apr-2012 15:03:24 GMT”,因为所有带有连字符的示例仅使用 2 位数字年份。