【发布时间】:2017-10-17 07:32:53
【问题描述】:
我想发送一个 post/get 请求,并且可以将 cookie 存储在 Program 附近的一个文件中。
示例 PHP 代码:
$___path="";
function send($url,$fields=array(),$reffer=false,$get=false)
{
global $___path;
$cookie_file_path = $___path."cookie.txt";//this file of cookies.
$agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1";
$ch = curl_init();
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if($reffer == false){}
else
{
curl_setopt($ch,CURLOPT_REFERER,$reffer);
}
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_URL, $url);
if($get)
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
}
else
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
if(count($fields) >= 1)
{
$POSTFIELDS = http_build_query($fields);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
}
}
$result = curl_exec($ch);
return $result;
}
可以帮助我使用 cookie jar store 吗?
这没有样本:https://github.com/juju/persistent-cookiejar
可以写一个关于https://golang.org/pkg/net/http/#CookieJar 的样本吗?
【问题讨论】:
-
cj := http.CookieJar{}client := http.Client{Jar: cj}client.Get(...) -
文件名路径在哪里?!这如何将cookie存储在文件中?可以说示例代码吗?