研究 go-curl 中的 OPT_COOKIEJAR 选项

时间:2024-02-18

浏览:371

作者:佚名

本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《研究 go-curl 中的 OPT_COOKIEJAR 选项》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~

问题内容

在go-curl中使用opt_cookiejar选项时,有没有办法检查工作是否完成?

如果运行下面的源代码,会出现在cookie下载完成之前执行readfile的问题。我想解决这个问题。

CommonSetopt(easy)
  easy.Setopt(curl.OPT_VERBOSE, 0)
  easy.Setopt(curl.OPT_COOKIEJAR, cookieName)
  easy.Setopt(curl.OPT_URL, string("https://drive.google.com/uc?export=download&id="+gdriveID))

  log.Println("start download cookie: ", url)
  if err := easy.Perform(); err != nil {
      log.Println("cookie download fail: ", err)
      return
  }

  readBuf, err := ioutil.ReadFile(cookieName)
  if err != nil {
      log.Println("cookie read fail: ", err)
      return
  }


正确答案


您需要在 easy.perform 之后立即清理会话,而不是使用 defer 语句进行清理

  CommonSetopt(easy)
  easy.Setopt(curl.OPT_VERBOSE, 0)
  easy.Setopt(curl.OPT_COOKIEJAR, cookieName)
  easy.Setopt(curl.OPT_URL, string("https://drive.google.com/uc?export=download&id="+gdriveID))

  log.Println("start download cookie: ", url)
  if err := easy.Perform(); err != nil {
      log.Println("cookie download fail: ", err)
      easy.Cleanup()
      return
  }
  // do cleanup
  easy.Cleanup()
  readBuf, err := ioutil.ReadFile(cookieName)
  if err != nil {
      log.Println("cookie read fail: ", err)
      return
  }

好了,本文到此结束,带大家了解了《研究 go-curl 中的 OPT_COOKIEJAR 选项》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

本文内容来源于互联网,如有侵权请联系删除。

相关文章

更多

热门专题

更多
首页