如何在Golang中查看cookieJar的内容?

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

How to view the contents of a cookieJar in Golang?

问题

我正在尝试查看从对Google的请求中的cookieJar响应的内容:

package main

import (
	"fmt"
	"log"
	"net/http"
	"net/http/cookiejar"
	"net/url"
)

func main() {

	jar, err := cookiejar.New(nil)
	if err != nil {
		log.Fatal(err)
	}
	client := http.Client{Jar: jar}
	resp, err := client.Get("https://accounts.google.com/ServiceLogin?continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Ffeature%3Dsign_in_button%26hl%3Den%26action_handle_signin%3Dtrue%26next%3D%252F%26app%3Ddesktop&service=youtube&sacu=1&passive=1209600&ignoreShadow=0&acui=0#identifier")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(resp.Cookies())

	s := ".google.com"
	u, err := url.Parse(s)
	fmt.Println(client.Jar.Cookies(u))

}

响应是一个空数组,但我可以看到有设置了cookie。如何在Golang中查看cookieJar的内容?

英文:

I'm trying to view the contents of a cookieJar response from a request to google:

package main

import (
	"fmt"
	"log"
	"net/http"
	"net/http/cookiejar"
	"net/url"
)

func main() {

	jar, err := cookiejar.New(nil)
	if err != nil {
		log.Fatal(err)
	}
	client := http.Client{Jar: jar}
	resp, err := client.Get("https://accounts.google.com/ServiceLogin?continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Ffeature%3Dsign_in_button%26hl%3Den%26action_handle_signin%3Dtrue%26next%3D%252F%26app%3Ddesktop&service=youtube&sacu=1&passive=1209600&ignoreShadow=0&acui=0#identifier")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(resp.Cookies())

	s := ".google.com"
	u, err := url.Parse(s)
	fmt.Println(client.Jar.Cookies(u))

}

The response is a blank array but I can see that there are cookies being set. How do I view the contents of a cookieJar in Golang?

答案1

得分: 2

很抱歉,标准库中的 cookie jar 是完全不透明的,只能通过(非通配符)URL 进行查询。

英文:

Unfortunately not, the cookie jar in the std library is completely opaque and can be queried via (non-wildcard) URLs only.

答案2

得分: 0

尽管Volker在技术上是正确的,即net/http/cookiejar实现是不透明的,但http.Client.Jar字段是一个接口(http.Client.CookieJar)。

因此,可以定义和使用一个提供列表功能或公开URL到cookie映射的实现。这样的实现应该基于标准库的实现,以利用其实现的安全功能。

如果有兴趣,我可以提供一些示例代码。

英文:

Although Volker is technically correct in the sense that the net/http/cookiejar implementation is opaque, the http.Client.Jar field is an interface (http.Client.CookieJar).

So it is possible to define and use an implementation that provides a list function or exposes a map of URL to cookies. Such an implementation should be based on the std library's implementation to leverage the security features it implements.

If there's any interest, I might provide some sample code.

huangapple
  • 本文由 发表于 2016年2月24日 13:24:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/35593920.html
匿名

发表评论

匿名网友

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

确定