Redirect Wikipedia url在10次重定向错误GoLang后停止

Redirect Wikipedia url在10次重定向错误GoLang后停止,redirect,go,https,Redirect,Go,Https,在执行HTTP Get请求时,我收到以下错误: 2015/08/30 16:42:09 Get https://en.wikipedia.org/wiki/List_of_S%26P_500_companies: stopped after 10 redirects 在以下代码中: package main import ( "net/http" "log" ) func main() { response, err := http.Get("https://en

在执行HTTP Get请求时,我收到以下错误:

2015/08/30 16:42:09 Get https://en.wikipedia.org/wiki/List_of_S%26P_500_companies: 
stopped after 10 redirects
在以下代码中:

package main

import (
    "net/http"
    "log"
)

func main() {
    response, err := http.Get("https://en.wikipedia.org/wiki/List_of_S%26P_500_companies")
    if err != nil {
        log.Fatal(err)
    }
}
我知道根据文件

// Get issues a GET to the specified URL. If the response is one of
// the following redirect codes, Get follows the redirect, up to a
// maximum of 10 redirects:
//
//    301 (Moved Permanently)
//    302 (Found)
//    303 (See Other)
//    307 (Temporary Redirect)
//
// An error is returned if there were too many redirects or if there
// was an HTTP protocol error. A non-2xx response doesn't cause an
// error.
我希望有人知道这种情况下的解决方案。看起来很奇怪,这个简单的url会导致超过十个重定向。让我觉得幕后可能会有更多的事情发生


谢谢。

正如其他人所指出的,您应该首先考虑为什么会遇到如此多的HTTP重定向。Go的默认策略是停止10次重定向,这是合理的。超过10个重定向可能意味着您处于重定向循环中。这可能是在代码之外引起的。它可能是由您的网络配置、您与网站之间的代理服务器等引起的

这就是说,如果确实需要更改默认策略,则不需要按照某人的建议编辑net/http源

要更改重定向的默认处理,您需要创建一个客户端并设置CheckRedirect

供参考:


我对包含
%26
的Wikipedia URL有这个问题,因为它们重定向到带有
&
的URL版本,然后将其编码到
%26
,Wikipedia将其重定向到
&
,并且

奇怪的是,从我的拱形盒子中移除
gcc go
(v1.4)并将其替换为
go
(v1.5)解决了这个问题


我猜这可以归因于v1.4和v1.5之间
net/http
的变化

如果您只是想解决这个问题,我建议更改http包的源代码。我感觉这里有点不对劲。不仅从telnet/curl发出的请求不需要一个重定向就可以工作,而且您的示例程序(一旦我使用response做了一些事情)在没有任何重定向的情况下也可以工作。你是否正在通过某种代理,这就是导致重定向的原因?使用curl是否有效?他得到了一个重定向循环。检查重定向不会阻止任何导致服务器发送重定向的事情。这一点很好。我没有想到为什么他会遇到10次重定向的默认策略。我将编辑我的答案。
// If CheckRedirect is nil, the Client uses its default policy,
// which is to stop after 10 consecutive requests.
CheckRedirect func(req *Request, via []*Request) error