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

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

How can make a Curl Store Cookie in Golang?

问题

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

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

  1. $___path = "";
  2. function send($url, $fields = array(), $reffer = false, $get = false)
  3. {
  4. global $___path;
  5. $cookie_file_path = $___path . "cookie.txt"; // 存储cookie的文件路径
  6. $agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1";
  7. $ch = curl_init();
  8. $headers[] = "Accept: */*";
  9. $headers[] = "Connection: Keep-Alive";
  10. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  11. curl_setopt($ch, CURLOPT_HEADER, 0);
  12. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  13. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  14. if ($reffer == false) {
  15. } else {
  16. curl_setopt($ch, CURLOPT_REFERER, $reffer);
  17. }
  18. curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  19. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  20. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  21. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
  22. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
  23. curl_setopt($ch, CURLOPT_URL, $url);
  24. if ($get) {
  25. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  26. } else {
  27. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  28. curl_setopt($ch, CURLOPT_POST, 1);
  29. if (count($fields) >= 1) {
  30. $POSTFIELDS = http_build_query($fields);
  31. curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
  32. }
  33. }
  34. $result = curl_exec($ch);
  35. return $result;
  36. }

这段代码可以帮助你使用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:

  1. $___path="";
  2. function send($url,$fields=array(),$reffer=false,$get=false)
  3. {
  4. global $___path;
  5. $cookie_file_path = $___path."cookie.txt";//this file of cookies.
  6. $agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1";
  7. $ch = curl_init();
  8. $headers[] = "Accept: */*";
  9. $headers[] = "Connection: Keep-Alive";
  10. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  11. curl_setopt($ch, CURLOPT_HEADER, 0);
  12. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  13. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  14. if($reffer == false){}
  15. else
  16. {
  17. curl_setopt($ch,CURLOPT_REFERER,$reffer);
  18. }
  19. curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  20. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  21. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  22. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
  23. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
  24. curl_setopt($ch, CURLOPT_URL, $url);
  25. if($get)
  26. {
  27. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  28. }
  29. else
  30. {
  31. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  32. curl_setopt($ch, CURLOPT_POST, 1);
  33. if(count($fields) >= 1)
  34. {
  35. $POSTFIELDS = http_build_query($fields);
  36. curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
  37. }
  38. }
  39. $result = curl_exec($ch);
  40. return $result;
  41. }

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 进程运行即可。如果你真的想要保存到文件中,可以按照以下步骤进行操作:

  1. import "net/http/cookiejar"
  2. import "net/http"
  3. import "encoding/json"
  4. import "io/ioutil"
  5. cookies := make(map[string][]*http.Cookie)
  6. jar, _ := cookiejar.New(nil)
  7. client := http.Client{Jar: jar}
  8. r, _ := client.Get(/* 做你需要的操作 */)
  9. siteCookies := jar.Cookies(r.Request.URL)
  10. cookies[r.Request.URL.String()] = siteCookies
  11. data, _ := json.Marshal(cookies)
  12. 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:

  1. import "net/http/cookiejar"
  2. import "net/http"
  3. import "encoding/json"
  4. import "io/ioutil"
  5. cookies := make(map[string][]*http.Cookie)
  6. jar, _ := cookiejar.New(nil)
  7. client := http.Client{Jar: jar}
  8. r, _ := client.Get(/* do whatever */)
  9. siteCookies := jar.Cookies(r.Request.URL)
  10. cookies[r.Request.URL.String()] = siteCookies
  11. data, _ := json.Marshal(cookies)
  12. 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:

确定