http - Golang http。获得太多重定向

标签 http go download

我正在尝试从网上下载文件。它应该是一个简单的过程。我以前做过的一个。但是,这个特定的链接(一个135 kB的zip文件)给我一个错误消息:Get "http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_megase.zip": stopped after 10 redirect。如果我将链接复制到浏览器中,则文件下载没有任何问题,但是使用下面的代码时,错误弹出。

package main

import (
    "io"
    "net/http"
    "os"
)

func main() {
    link := "http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_megase.zip"
    resp, err := http.Get(link)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    // Create the file
    out, err := os.Create("ms.zip")
    if err != nil {
        panic(err)
    }
    defer out.Close()

    // Write the body to file
    _, err = io.Copy(out, resp.Body)
    if err != nil {
        panic(err)
    }
}


关于为什么会发生以及如何解决的任何想法?

感谢您的关注。

最佳答案

研究此网址后,我看到它设置了cookieSet-Cookie: security=true; path=/
您可以手动设置cookie或实现CookieJar

    c := http.Client{}
    req, err := http.NewRequest("GET", link, nil)
    if err != nil {
        panic(err)
    }
    req.AddCookie(&http.Cookie{Name: "security", Value: "true", Path: "/"})

    resp, err := c.Do(req)
    if err != nil {
        panic(err)
    }

关于http - Golang http。获得太多重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61255935/

相关文章:

Java URL - Google 翻译请求返回 403 错误?

http - Golang根据struct字段设置http响应码

go - 您应该使用 protobuf 作为用于处理还是仅用于传输的数据类型?

javascript - 使用 javascript/jquery 下载文件

Python:从 FTP 服务器下载文件

javascript - 使用 iOS 通过 Javascript 下载网站上的文件

http - 配置 nginx 以从单个服务器提供 HTTP 和 HTTPS 站点的麻烦

android - 使用 Intent 过滤器从 URL 打开 Android 应用程序不起作用

go - 为什么我不能在 go 中将 `func() []int` 作为 `func() []interface{}` 传递?

java - 将剪辑路径信息添加到图像