如何在Golang中创建一个Curl存储Cookie的功能?

huangapple go评论162阅读模式
英文:

How can make a Curl Store Cookie in Golang?

问题

我想发送一个POST/GET请求,并且可以将cookie存储在程序附近的一个文件中。

以下是一个示例的PHP代码:

$___path = "";
function send($url, $fields = array(), $reffer = false, $get = false)
{
    global $___path;
    $cookie_file_path = $___path . "cookie.txt"; // 存储cookie的文件路径
    $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存储。

关于Go语言的示例,你可以参考net/http包中的CookieJar文档:https://golang.org/pkg/net/http/#CookieJar

希望对你有帮助!

英文:

I want send a post/get request and can store cookies in a file near on Program.

Sample Php Code:

$___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;
}

may help me to work with cookie jar store?

this not have a sample : https://github.com/juju/persistent-cookiejar

may a person write a sample about https://golang.org/pkg/net/http/#CookieJar ?

答案1

得分: 2

你不需要将 cookies 保存到文件中,只需保持你的 Go 进程运行即可。如果你真的想要保存到文件中,可以按照以下步骤进行操作:

import "net/http/cookiejar"
import "net/http"
import "encoding/json"
import "io/ioutil"

cookies := make(map[string][]*http.Cookie)
jar, _ := cookiejar.New(nil)
client := http.Client{Jar: jar}
r, _ := client.Get(/* 做你需要的操作 */)
siteCookies := jar.Cookies(r.Request.URL)
cookies[r.Request.URL.String()] = siteCookies
data, _ := json.Marshal(cookies)
ioutil.WriteFile("/path/to/cookies.txt", data, 0644)

然后在启动时,你可以读取该文件,对于每个键调用 url.Parse,然后使用它调用 jar.SetCookies

https://play.golang.org/p/gQ_n6rPsdl

英文:

You don't need to save cookies to a file, just keep your go process running. If you really wanted to, you would do something like:

import "net/http/cookiejar"
import "net/http"
import "encoding/json"
import "io/ioutil"

cookies := make(map[string][]*http.Cookie)
jar, _ := cookiejar.New(nil)
client := http.Client{Jar: jar}
r, _ := client.Get(/* do whatever */)
siteCookies := jar.Cookies(r.Request.URL)
cookies[r.Request.URL.String()] = siteCookies
data, _ := json.Marshal(cookies)
ioutil.WriteFile("/path/to/cookies.txt", data, 0644)

And then when you start, you would read that file, for each key call url.Parse, and then use that to call jar.SetCookies.

https://play.golang.org/p/gQ_n6rPsdl

huangapple
  • 本文由 发表于 2017年5月18日 01:42:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/44031601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定