http - Go http客户端不遵循重定向

标签 http go

我有一个检查 url 的函数:

func Checkurl(url string) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
resp, err := client.Get(url)

错误变量:意外的 EOF

在 tcpdump 中我看到重定向和连接关闭:

15:53:41.510722 IP (tos 0x0, ttl 248, id 18315, offset 0, flags [none], proto TCP (6), length 123)
XXX.XXX.XXX.XXX.80 > XXX.XXX.XXX.XXX.53618: Flags [F.], cksum 0xb96f (correct), seq 85:168, ack 1, win 5840, length 83: HTTP, length: 83
HTTP/1.1 302 Moved Temporarily
Location: http://XXX.XXX.XXX.XXX
Connection: close

如何获取位置?

最佳答案

使用Response.Location功能。

newUrl, err := resp.Location()

输入 Client支持重定向。请参阅 CheckRedirect 字段。这可能是处理重定向的更好工具,具体取决于您要实现的目标。

Request对象方法使用client.Do

client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
// check error
resp, err := client.Do(req)
// check error
newURL, err := resp.Location()
// check error

默认客户端默认处理重定向,因此我会检查最终目的地的响应。这很可能是您问题的根本原因。

关于http - Go http客户端不遵循重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43520655/

相关文章:

http - 用于Web HTTP/HTTPS的基于永久ID的超链接方案,有多少种方法?

pointers - 类型将接口(interface)指针转换为接口(interface)指针

google-app-engine - 将 Go 应用程序拆分为 GoLang 的多个文件

http - 带嵌套参数的 POST 请求

http - 已存在资源但不存在实体的正确 HTTP 状态代码?

http - Nginx http 和 https block 配置

docker - CURL在Docker镜像中不起作用[无法访问Docker镜像中的主机]

sorting - 真的可以对 map 进行排序吗?

memory - 戈朗 : trouble with memory

c# - C# 中的超小型 Web 服务器