天天看点

Apache httpclient的execute方法调试

因为工作需要,想研究一下execute执行的逻辑。

在这一行调用execute:

response = getHttpClient().execute(get);           

getHttpClient的实现:

private HttpClient getHttpClient() {
        if (this.m_httpClient == null) {
            this.m_httpClient = HttpClientBuilder.create().build();
        }
        return this.m_httpClient;
    }           

我在代码里声明的HttpClient只是一个接口,

Apache httpclient的execute方法调试

实现类是InternalHttpClient。

Apache httpclient的execute方法调试

首先根据传入的请求决定出目标-target host

Apache httpclient的execute方法调试

投递到RedirectExec执行。

Apache httpclient的execute方法调试

后者又投递到RetryExec执行。

Apache httpclient的execute方法调试

收到307重定向:

Apache httpclient的execute方法调试

redirectsEnabled标志位为true:

Apache httpclient的execute方法调试

再看当前的请求确实被redirect了吗?

Apache httpclient的execute方法调试

original url:

Apache httpclient的execute方法调试

我的后台服务器返回的307,落到了分支HttpStatus.SC_TEMPORARY_REDIRECT处:

Apache httpclient的execute方法调试

看来Apache的库认为只有HEAD和GET才能被redirect:

Apache httpclient的execute方法调试

重定向最大次数:50

Apache httpclient的execute方法调试

准备重试了:

Apache httpclient的execute方法调试

本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

继续阅读