1 回答

TA贡献1780条经验 获得超4个赞
您需要检查重定向并停止(捕获)它们。如果捕获重定向,则可以使用响应结构的 location 方法获取重定向 URL(发生重定向的目标)。
package main
import (
"errors"
"fmt"
"net/http"
)
func main() {
req, err := http.NewRequest("GET", "https://www.google.com", nil)
if err != nil {
panic(err)
}
client := new(http.Client)
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return errors.New("Redirect")
}
response, err := client.Do(req)
if err == nil {
if response.StatusCode == http.StatusFound { //status code 302
fmt.Println(response.Location())
}
} else {
panic(err)
}
}
没有找到匹配的内容?试试慕课网站内搜索吧
- 1 回答
- 0 关注
- 399 浏览
添加回答
举报