为什么golang只能看到一个Cookie,而我在Firefox中可以看到更多的Cookie?

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

Why does golang only see 1 of the Cookies but i can see more in firefox

问题

以下是翻译好的内容:

// 代码示例如下,我尝试了几种方法,包括使用 cookie jar,但都没有成功,这是最简单的示例:

// 也尝试了这个链接的方法:https://stackoverflow.com/questions/27206746/go-http-client-not-returning-cookies

// 结果都一样。

// 如果你运行这段代码,你会发现只有一个 cookie,但在浏览器中有更多的 cookie。

var url = "http://www.apple.com"

func main() {

    // 创建客户端
    client := http.Client{}

    // 创建变量

    // 创建请求
    req, _ := http.NewRequest("GET", url, nil)
    resp, err := client.Do(req) // 发送请求
    if err != nil {
        return
    }
    cookies := resp.Cookies() // 保存 cookie

    // 创建新请求
    resp, err = client.Do(req) // 发送请求
    if err != nil {
        return
    }

    fmt.Println("------------------------------------------------")

    for _, cookie := range cookies {
        fmt.Println(cookie)
    }
}
英文:

Code below I have tried several methods using cookie jar etc to no avail, this is the simplest example:

also tried this https://stackoverflow.com/questions/27206746/go-http-client-not-returning-cookies

with the same results.

if you run the code you can see only one cookie but in the browser there are several more

var url = "http://www.apple.com"

func main() {

	//Create Client

	client := http.Client{}
	//Create Varible

	// Create Request

	req, _ := http.NewRequest("GET", url, nil)
	resp, err := client.Do(req) //send request
	if err != nil {
		return
	}
	cookies := resp.Cookies() //save cookies
	// Create New Request

	resp, err = client.Do(req) //send request
	if err != nil {
		return
	}

	fmt.Println("------------------------------------------------")

	for _, cookie := range cookies {
		fmt.Println(cookie)
	}
}

答案1

得分: 2

您正在对http://www.apple.com的文档进行单个请求;对此的响应将是一个HTML文件和一些头信息。完整的响应将取决于许多因素(如您的位置和请求头),但对我来说,这返回一个单独的cookie:

set-cookie: geo=NZ; path=/; domain=.apple.com

这就是您的Go应用程序所做的全部。

浏览器发出相同的请求,但然后处理返回的HTML;作为此过程的一部分,浏览器将请求更多的文件(cssjssvgpngwoff2等)。这些请求的响应可能包含cookie。如果您在浏览器中打开开发者工具(通常是F12),并在刷新页面之前选择网络选项卡,您就可以看到这一点。

例如,当我打开页面时,请求的其中一个文件是https://www.apple.com/shop/experience-meta,对此的响应包含五个cookie(as_dcas_pctsas_xsdssfdssid2)。这就是您看到的额外cookie的来源(可能还有来自先前访问的cookie)。

如果您想在Go中检索所有这些cookie,您需要解析HTML(包括运行任何包含的JavaScript)并请求其他文件。但请注意,即使您这样做,您的结果可能会有所不同(取决于您发送的请求头和苹果考虑的其他因素)。

英文:

You are making a single request for the document at http://www.apple.com; the response to this will be an HTML file and some headers. The full response will depend upon a number of factors (such as your location and request headers) but for me this returns a single cookie:

set-cookie: geo=NZ; path=/; domain=.apple.com

This is all your Go application does.

The browser makes the same request but then processes the returned HTML; as part of this process the browser will request quite a few more files (css, js,svg, png, woff2 etc). The responses to these requests may include cookies. You can see this if you open the Developer Tools in your browser (generally F12) and select the Network tab before refreshing the page.

For example when I open the page one of the files requested is https://www.apple.com/shop/experience-meta and the response to this includes five cookies (as_dc, as_pcts, as_xs, dssf and dssid2). This is where the extra cookies you are seeing will come from (well, potentially, there may already be cookies from a previous visit too).

If you want to retrieve all of these cookies in Go you will need to parse the HTML (including running any included JavaScript) and request the additional files. But please note that even if you do this your results may differ (depending upon the request headers you send and whatever other factors Apple takes into account).

答案2

得分: 1

我不确定apple.com的确切情况。但是很多网站在给你访问/cookies之前会查看一些内容。
例如,它们会查看你使用的浏览器、你的头部信息等等。你可以尝试添加一些头部信息,比如User-Agent、Accept等。同时,请确保你的操作不违反苹果的服务条款。希望这可以帮到你!

英文:

I'm not sure about the exact case of apple.com. But a lot of websites look at several things before giving you access / cookies.
For example which browser you're using, your headers and others. You could try adding some headers like User-Agent, Accept and such. Also make sure what you're doing is not against Apple's ToS. Hope this helps!

huangapple
  • 本文由 发表于 2022年7月15日 02:37:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/72985245.html
匿名

发表评论

匿名网友

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

确定