HttpClient出現TCP鏈接異常關閉發送RST包

問題現象

在與第三方系統經過http交互數據的過程當中,抓包發現每次TCP鏈接都是異常關閉,報文以下:
輸入圖片說明html

能夠看到,由我方發起3次握手創建鏈接,而後發送http請求,對方響應數據,我方ACK後直接發RST包(圖中藍色陰影部分),將鏈接異常關閉,不只沒有複用鏈接,且不是經過4次揮手來關閉鏈接。java

關於RST包參考:http://blog.csdn.net/erlib/ar...git

問題緣由

HttpClient 4.5.2 釋放鏈接API使用方式不正確github

問題代碼以下(已簡化):tcp

private boolean isCheckSuccess() {
       CloseableHttpResponse response = null; 
       try {
            // 經過HttpClientBuilder建立CloseableHttpClient
            httpClient = HttpClientFactory.createHttpClient();
            request = initCheckRequest();
            response = httpClient.execute(request);
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)  {
                return true;
            }
        }  finally {
            // 釋放鏈接
            if(request != null) {
                request.releaseConnection();
            }
        }
        return false;
    }

     //構造請求參數
    private static HttpPost initCheckRequest() throws IOException {
        HttpPost request = new HttpPost(callBackUrl);
        request.setHeader("contentType", ContentTypeEnum.JSON.getDesc());
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("developer_id", "111"));
        params.add(new BasicNameValuePair("sign", "sign"));
        HttpEntity postParams = new UrlEncodedFormEntity(params);
        request.setEntity(postParams);
        return request;
    }

根據釋放鏈接處代碼和抓包分析來看,request.releaseConnection(); 每次是發TCP RST包來關閉鏈接post

解決方案

既然是釋放鏈接不對,則修改代碼爲以下:ui

finally {
            if(response != null) {
                try {
                    EntityUtils.consume(response.getEntity());
                    response.close();
                } catch (IOException e) {
                    logger.error("close http error", e);
                }
            }
        }

正確方式爲:
關閉內容流後再關閉響應自己。spa

若是不關閉IO流,直接response.close(); 則結果是鏈接不能複用,直接經過4次揮手斷開TCP鏈接。.net

鏈接池釋放鏈接的時候,並不會直接對TCP鏈接的狀態有任何改變,只是維護了兩個Set,leased和avaliabled,leased表明被佔用的鏈接集合,avaliabled表明可用的鏈接的集合,釋放鏈接的時候僅僅是將鏈接從leased中remove掉了,並把鏈接放到avaliabled集合中code

本人blog:https://my.oschina.net/hebaod...

參考

TCP RST相關
http://www.cnblogs.com/JohnAB...
http://blog.csdn.net/erlib/ar...
http://lovestblog.cn/blog/201...
https://my.oschina.net/costax...

HttpClient相關
https://www.cnblogs.com/likai...
http://liangbizhi.github.io/h...

相關文章
相關標籤/搜索