使用Gorilla工具包时出现无限重定向循环。

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

Infinite redirect loop with Gorilla toolkit

问题

我有这段简单的代码:

import (
   "log"
   "github.com/gorilla/http"
   "bytes"
)

func main() {
 url := "https://www.telegram.org"
 log.Println("url: " + url)
 var b bytes.Buffer
 http.Get(&b, url)
 log.Println("Get done")
}

它在进行GET请求的那一行卡住了。看起来它进入了一个无限循环的302响应,该响应将重定向到相同的URL("https://www.telegram.org")。
我是不是做错了什么或者做出了错误的假设?

谢谢和问候。

英文:

I have this simple code:

import (
   "log"
   "github.com/gorilla/http"
   "bytes"
)

func main() {
 url := "https://www.telegram.org"
 log.Println("url: " + url)
 var b bytes.Buffer
 http.Get(&b, url)
 log.Println("Get done")
}

and it freezes on the line making the GET request. It seems that it enters an infinite loop of 302 responses which redirects to the same url ("https://www.telegram.org").
Am I doing or assuming something wrong?

Thanks and regards.

答案1

得分: 2

显然,该库不支持https(哈哈)

https://github.com/gorilla/http/issues/8

所以只需使用stdlib的http模块:

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
)

func main() {

	res, err := http.Get("https://www.telegram.org")
	if err != nil {
		return
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return
	}

	fmt.Printf("%s", body)

}
英文:

Apparently that library doesn't support https (lol)

https://github.com/gorilla/http/issues/8

So just use the stdlib http module:

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
)

func main() {

	res, err := http.Get("https://www.telegram.org")
	if err != nil {
		return
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return
	}

	fmt.Printf("%s", body)

}

huangapple
  • 本文由 发表于 2016年1月31日 12:27:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/35110335.html
匿名

发表评论

匿名网友

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

确定