英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论