gpt4 book ai didi

http - 你如何在golang中指定http referrer?

转载 作者:IT王子 更新时间:2023-10-29 01:52:16 32 4
gpt4 key购买 nike

使用标准的 http.Client,您如何构建在 http 请求 header 中指定引荐来源网址的 Web 请求?

下面你可以看到可以设置headers,但是如何指定referer呢?是不是只要设置一个Referer header?

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
req.Header.Set("Accept", "text/html,application/xhtml+xml")
req.Header.Set("User-Agent", "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1)")

response, err1 := client.Get(url)
if err1 != nil {
return "", err1
}

最佳答案

是的,正如您从 Go 本身的来源中看到的那样,在 src/net/http/client.go

// Add the Referer header from the most recent
// request URL to the new one, if it's not https->http:
if ref := refererForURL(reqs[len(reqs)-1].URL, req.URL); ref != "" {
req.Header.Set("Referer", ref)
}

虽然检查你的方案,as in the same sources :

// refererForURL returns a referer without any authentication info or
// an empty string if lastReq scheme is https and newReq scheme is http.
func refererForURL(lastReq, newReq *url.URL) string {
// https://tools.ietf.org/html/rfc7231#section-5.5.2
// "Clients SHOULD NOT include a Referer header field in a
// (non-secure) HTTP request if the referring page was
// transferred with a secure protocol."
if lastReq.Scheme == "https" && newReq.Scheme == "http" {
return ""
}

关于http - 你如何在golang中指定http referrer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46086746/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com