因为工作需要,想研究一下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方法调试-LMLPHP

实现类是InternalHttpClient。

Apache httpclient的execute方法调试-LMLPHP

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

Apache httpclient的execute方法调试-LMLPHP

投递到RedirectExec执行。

后者又投递到RetryExec执行。

收到307重定向:

redirectsEnabled标志位为true:

Apache httpclient的execute方法调试-LMLPHP

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

Apache httpclient的execute方法调试-LMLPHP

original url:

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

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

重定向最大次数:50

准备重试了:

Apache httpclient的execute方法调试-LMLPHP

要获取更多Jerry的原创文章,请关注公众号"汪子熙":


Apache httpclient的execute方法调试-LMLPHP
12-04 14:23
查看更多