【发布时间】:2016-12-20 17:58:09
【问题描述】:
我正在尝试从一个使用 netscape HTTP cookie 文件登录的旧站点获取信息。这是我的 curl 请求:
// Do login request and get cookie
curl -c cookies -X POST -i -v https://foobar.com/login
// Use generated cookie file to get more data about the user
curl -b cookies -i -v https://foobar.com/data
在 PHP 中,您可以执行以下操作:
// Do login request and get cookie
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://foobar/login');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, './cookies');
curl_setopt($ch, CURLOPT_COOKIEFILE, './cookies');
$user = curl_exec($ch);
// Use generated cookie file to get data about the user
curl_setopt($ch, CURLOPT_URL, 'https://foobar/login');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, './cookies');
curl_setopt($ch, CURLOPT_COOKIEFILE, './cookies');
$data = curl_exec($ch);
有没有办法在 Go 中使用 std http 包来做到这一点?
【问题讨论】:
-
使用
http.CookieJar管理多个请求之间的cookie。介绍见相关问题What is the difference between cookie and cookiejar?