apache httpclient4 設置超時時間java
舊的方法(已被禁用)apache
CloseableHttpClient httpclient = HttpClients.createDefault(); httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,5000); httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,5000);
新的方法post
HttpPost httppost = new HttpPost(httpUrl); //設置超時時間爲5秒 RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5000).setConnectTimeout(5000).setSocketTimeout(5000).build(); httppost.setConfig(requestConfig);