关于 go-curl 的 OPT_COOKIEJAR 选项的问题。

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

Questions about go-curl's OPT_COOKIEJAR option

问题

在使用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
}
英文:

When using the OPT_COOKIEJAR option in go-curl, is there a way to check whether the work is completed?

If you run the source code below, there is a problem that ReadFile is executed before the cookie download is completed. I want to solve this.

  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
  }

答案1

得分: 2

你需要在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("开始下载cookie: ", url)
  if err := easy.Perform(); err != nil {
      log.Println("cookie下载失败: ", err)
      easy.Cleanup()
      return
  }
  // 进行清理
  easy.Cleanup()
  readBuf, err := ioutil.ReadFile(cookieName)
  if err != nil {
      log.Println("cookie读取失败: ", err)
      return
  }
英文:

You need to cleanup the session immediately after easy.Perform instead of cleaning up using a defer statement

  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
  }

huangapple
  • 本文由 发表于 2021年11月2日 14:40:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/69806290.html
匿名

发表评论

匿名网友

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

确定