前言
这是个3.X的超时设置方法
HttpClient client = newHttpClient(); client.setConnectionTimeout(30000); client.setTimeout(30000);
还可以这样
HttpClient httpClient = newHttpClient(); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(3000);
4.X版本的超时设置(4.3之前版本)
HttpClient httpClient=newDefaultHttpClient(); httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,2000);//连接时间 httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,2000);//数据传输时间
4.3及以后版本的超时设置
CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = newHttpGet(url); RequestConfig config = RequestConfig.custom().setConnectTimeout(2000).setConnectionRequestTimeout(1000).setSocketTimeout(2000).build(); httpGet.setConfig(config);
示例
下面以4.5版写一个例子:
所需jar包
<!-- httpclient--> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.3.1</version> </dependency>
示例代码
package com.site.utils; import org.apache.commons.lang.StringUtils; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.LinkedList; import java.util.List; import java.util.Map; /** * 网络请求工具类 */ public class HttpUtil { private static Logger logger = LoggerFactory.getLogger(HttpUtil.class); //日志记录 /** * post请求 * * @param url url地址 * @param paramMap 参数 * @return */ public static String httpPost(String url,Map<String, String> paramMap) { logger.info("post url-->" + url); try { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); //设置超时 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(1000).setSocketTimeout(5000).build(); httpPost.setConfig(requestConfig); //设置请求参数 if (!paramMap.isEmpty()) { List<NameValuePair> list = new LinkedList<>(); for (Map.Entry<String, String> entry : paramMap.entrySet()) { list.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list, "UTF-8"); httpPost.setEntity(formEntity); } CloseableHttpResponse result = httpClient.execute(httpPost); String response = EntityUtils.toString(result.getEntity(), "UTF-8"); logger.info("response-->" + response); return response; } catch (IOException e) { logger.error("post请求失败:", e); } return null; } /** * 发送get请求 * * @param url 路径 * @return */ public static String httpGet(String url) { logger.info("get url-->" + url); try { CloseableHttpClient client = HttpClients.createDefault(); //发送get请求 HttpGet request = new HttpGet(url); //设置超时 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(1000).setSocketTimeout(5000).build(); request.setConfig(requestConfig); CloseableHttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { /**请求发送成功,并得到响应**/ String resp = EntityUtils.toString(response.getEntity(), "UTF-8"); logger.info("response-->" + resp); return StringUtils.isBlank(resp) ? "OK" : resp; } } catch (IOException e) { logger.error("get请求提交失败:", e); } return null; } }
setConnectTimeout
:设置连接超时时间,单位毫秒。setConnectionRequestTimeout
:设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。setSocketTimeout
:请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
参考: