go - Gorilla 工具包的无限重定向循环

标签 go gorilla

我有这个简单的代码:

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”)。 我是在做错事还是在假设错事?

感谢和问候。

最佳答案

显然那个库不支持 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)

}

关于go - Gorilla 工具包的无限重定向循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35110335/

相关文章:

http - 戈朗 : how to make http client not following redirects?

Golang Gorilla CEX.IO Websocket 认证错误

linux - 如何运行心跳功能

golang 与 protobuf 与 dep

go - 在 jwt-go 中解析 JWT Auth token 时, key 类型无效

go - Go 结构中的无名字段?

go - 将 websocket 循环与 Golang 中的 channel 同步

Golang 如何从 html/template 访问 slice 字段

Golang gorilla mux 未找到处理程序无法正常工作

Go:使用 gorilla mux 提供 CSS 文件