gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 20:25:07 26 4
gpt4 key购买 nike

我正在尝试从网上下载文件。它应该是一个简单的过程。我以前做过的一个。但是,这个特定的链接(一个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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com