Apache httpclient的execute方法除錯

i042416發表於2019-05-26

因為工作需要,想研究一下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方法除錯

要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":




來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2645585/,如需轉載,請註明出處,否則將追究法律責任。

相關文章