我们可以注册CheckRedirect重定向请求时检查下一个请求。有没有办法在第一个请求重定向时获得响应?
最佳答案
它目前的实现方式,默认情况下似乎无法查看响应(除非您自己实现 Do()
的功能)。
参见 src/net/http/client.go#L384-L399
:
if shouldRedirect(resp.StatusCode) {
// Read the body if small so underlying TCP connection will be re-used.
// No need to check for errors: if it fails, Transport won't reuse it anyway.
const maxBodySlurpSize = 2 << 10
if resp.ContentLength == -1 || resp.ContentLength <= maxBodySlurpSize {
io.CopyN(ioutil.Discard, resp.Body, maxBodySlurpSize)
}
resp.Body.Close()
if urlStr = resp.Header.Get("Location"); urlStr == "" {
err = fmt.Errorf("%d response missing Location header", resp.StatusCode)
break
}
base = req.URL
via = append(via, req)
continue
}
关于http - 我可以在重定向请求时检查响应吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29865691/