2017-09-07 18 views
0

標準http.Clientを使用すると、httpリクエストヘッダーのリファラーを指定するWebリクエストをどのように構築しますか?golangでhttpリファラーをどのように指定しますか?

以下はヘッダーを設定することができますが、どのようにしてリファラーを指定しますか? Refererヘッダーを設定するだけですか?

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 
} 
+2

ます。https://tools.ietf。 org/html/rfc2616#section-14.36 –

+7

はい、Refererはヘッダーです。あなたはそれを設定しようとしましたか? – JimB

+0

@JimBもし私がそれを設定する方法を知っていたら、私はこの質問をする必要はありませんでした。 :D – Jacob

答えて

2

はい、あなたは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 "" 
} 
関連する問題