简体   繁体   English

从 URL 获取 cookie

[英]Get cookie from URL

I am wondering if there is a way to get a cookie from a URL using golang?我想知道是否有一种方法可以使用 golang 从 URL 获取 cookie? I have tried to use a few examples that come up when I google it but it never seems to return the cookie我尝试使用谷歌搜索时出现的一些示例,但它似乎永远不会返回 cookie

This seems to do it:这似乎做到了:

package http
import "net/http"

func get_cookie(ref, name string) (*http.Cookie, error) {
   res, err := http.Get(ref)
   if err != nil {
      return nil, err
   }
   defer res.Body.Close()
   for _, cook := range res.Cookies() {
      if cook.Name == name {
         return cook, nil
      }
   }
   return nil, http.ErrNoCookie
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM