出現SocketTimeoutException后一直無法在連接服務器


在做接入sdk功能的時候,經常出現一個問題,內網向外網服務器建立連接並發送數據經常會報SocketTimeoutException這個錯誤,且一旦出現便大幾率再也連不上了。修改之前的代碼為:

    public static String httpPost(String url, Map<String, String> paramMap, int timeout)
            throws HttpException, IOException {

        List<NameValuePair> valueList = new ArrayList<NameValuePair>();
        Iterator<String> it = paramMap.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            String value = paramMap.get(key);
            NameValuePair nv = new NameValuePair(key, value);
            valueList.add(nv);
        }

        NameValuePair[] valueArray = valueList.toArray(new NameValuePair[0]);

        String response = null;
        HttpClient client = new HttpClient();
        client.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);

        setTimeout(client, timeout);
        
        PostMethod method = new PostMethod(url);

        method.getParams().setContentCharset("UTF-8");
        method.setRequestBody(valueArray);

        int status = client.executeMethod(method);
        if (status != HttpStatus.SC_OK) {
            return response;
        }

        response = method.getResponseBodyAsString();

        return response;
    }

查了一些資料,試着捕獲異常后,清空連接池解決,修改后的代碼為:

public static String httpPost(String url, Map<String, String> paramMap, int timeout)
            throws HttpException, IOException {
        String response = " null ";
        HttpClient client = new HttpClient();
        try{
            List<NameValuePair> valueList = new ArrayList<NameValuePair>();
            Iterator<String> it = paramMap.keySet().iterator();
            while (it.hasNext()) {
                String key = it.next();
                String value = paramMap.get(key);
                NameValuePair nv = new NameValuePair(key, value);
                valueList.add(nv);
            }
    
            NameValuePair[] valueArray = valueList.toArray(new NameValuePair[0]);
    

            client.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
    
            setTimeout(client, timeout);
            
            PostMethod method = new PostMethod(url);
    
            method.getParams().setContentCharset("UTF-8");
            method.setRequestBody(valueArray);
    
            int status = client.executeMethod(method);
            if (status != HttpStatus.SC_OK) {
                return response;
            }
    
            byte[] res = method.getResponseBody();//.getResponseBodyAsString();
            response = new String(res);
        }catch(Exception e){
            closeIdleSocket(client);
            throw(e);
        }
        return response;
    }
    
    public static void closeIdleSocket(HttpClient client){
        client.getHttpConnectionManager().closeIdleConnections(50000);
    }
    

目前這樣修改之后暫時沒有在出現連接超時問題了,但無法確認這個就是根源所在,所以還在持續驗證中。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM