重定向请求后,我可以检查响应吗? [英] Can I check response when the request is redirected?

查看:94
本文介绍了重定向请求后,我可以检查响应吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以注册 CheckRedirect ,以在重定向请求时检查下一个请求.重定向后的第一个请求有没有办法得到我的响应?

We can register CheckRedirect to check the next request when the request is redirected. Is there a way that I can get the response for the first request when it's redirected?

推荐答案

当前的实现方式,默认情况下似乎无法查看响应(除非您自己实现Do()的作用) .
参见 src/net/http/client.go#L384-L399 :

The way it is currently implemented, it doesn't seem possible to have a look at the response by default (unless you implement yourself what Do() does).
See 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
    }

这篇关于重定向请求后,我可以检查响应吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆