1.一般需要如下几步:
(1) 创建HttpClient对象。
(2)创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
(3)
如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams
params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity
entity)方法来设置请求参数。
(4) 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
(5)
调用HttpResponse的getAllHeaders()、getHeaders(String
name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器
的响应内容。程序可通过该对象获取服务器的响应内容。
(6) 释放连接。无论执行方法是否成功,都必须释放连接
依赖:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
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.client.utils.URIBuilder;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; public class HttpClientUtils { // HTTP内容类型。相当于form表单的形式,提交数据
public static final String CONTENT_TYPE_JSON_URL = "application/json;charset=utf-8"; // utf-8字符编码
public static final String CHARSET_UTF_8 = "utf-8"; // 连接管理器
private static PoolingHttpClientConnectionManager pool; // 请求配置
private static RequestConfig requestConfig; static { try {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
builder.build());
// 配置同时支持 HTTP 和 HTPPS
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create
().register(
"http", PlainConnectionSocketFactory.getSocketFactory()).register(
"https", sslsf).build();
// 初始化连接管理器
pool = new PoolingHttpClientConnectionManager(
socketFactoryRegistry);
// 将最大连接数增加到200,实际项目最好从配置文件中读取这个值
pool.setMaxTotal(200);
// 设置最大路由
pool.setDefaultMaxPerRoute(2); requestConfig = requestConfig(); } catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
} public static CloseableHttpClient getHttpClient() { CloseableHttpClient httpClient = HttpClients.custom()
// 设置连接池管理
.setConnectionManager(pool)
// 设置请求配置
.setDefaultRequestConfig(requestConfig)
// 设置重试次数
.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false))
.build(); return httpClient;
} /**
* 构建请求配置信息
* 超时时间什么的
*/
private static RequestConfig requestConfig() {
// 根据默认超时限制初始化requestConfig
int socketTimeout = 10000;
int connectTimeout = 10000;
int connectionRequestTimeout = 10000;
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(connectTimeout) // 创建连接的最长时间
.setConnectionRequestTimeout(connectionRequestTimeout) // 从连接池中获取到连接的最长时间
.setSocketTimeout(socketTimeout) // 数据传输的最长时间
.setStaleConnectionCheckEnabled(true) // 提交请求前测试连接是否可用
.build();
return config;
} public static String doGetJson(String url, String param, Map<String, String> requestHead) {
String result = "";
CloseableHttpResponse response = null;
CloseableHttpClient httpclient = null;
try {
httpclient = getHttpClient();
URI uri = null;
if (param == null || param.equals("")) {
uri = new URIBuilder(url).build();
} else {
uri = new URIBuilder(url + "?" + param).build();
}
// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
httpGet.setConfig(requestConfig());
if (null != requestHead) {
for (Map.Entry<String, String> entry : requestHead.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
httpGet.addHeader(key, value);
}
}
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(response.getEntity(), CHARSET_UTF_8);
}
result = decodeData(result);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
//不可以关闭,不然连接池就会被关闭
//httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
} public static String doPostJson(String url, String param, Map<String, String> requestHead) {
// 创建Httpclient对象
CloseableHttpClient httpClient = getHttpClient();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建请求内容
StringEntity entity = new StringEntity(param, ContentType.APPLICATION_JSON);
entity.setContentType(CONTENT_TYPE_JSON_URL);
httpPost.setEntity(entity);
httpPost.setConfig(requestConfig());
if (null != requestHead) {
for (Map.Entry<String, String> entry : requestHead.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
httpPost.addHeader(key, value);
}
}
// 执行http请求
response = httpClient.execute(httpPost);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), CHARSET_UTF_8);
}
resultString = decodeData(resultString); } catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
} return resultString;
} public static String doGet(String url, Map<String, String> param, Map<String, String> requestHead) {
String result = "";
CloseableHttpResponse response = null;
CloseableHttpClient httpclient = null;
try {
String params = toHttpGetParams(param);
httpclient = getHttpClient();
URI uri = new URIBuilder(url + "?" + params).build();
// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
httpGet.setConfig(requestConfig());
if (null != requestHead) {
for (Map.Entry<String, String> entry : requestHead.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
httpGet.addHeader(key, value);
}
}
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(response.getEntity(), CHARSET_UTF_8);
}
result = decodeData(result);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
} public static String doGet(String url, Map<String, String> param) {
return doGet(url, param, null);
} /**
* 根据实际需要决定是否需要解码
*/
static String decodeData(String base64Data) {
String str = "";
if (base64Data == null || base64Data.equals("")) {
str = "";
}
try {
String e = new String(Base64.decodeBase64(base64Data.getBytes(CHARSET_UTF_8)), CHARSET_UTF_8);
return e;
} catch (UnsupportedEncodingException var5) {
}
return str;
} /**
* 这里只是其中的一种场景,也就是把参数用&符号进行连接且进行URL编码
* 根据实际情况拼接参数
*/
private static String toHttpGetParams(Map<String, String> param) throws Exception {
String res = "";
if (param == null) {
return res;
}
for (Map.Entry<String, String> entry : param.entrySet()) {
res += entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), CHARSET_UTF_8) + "&";
}
return "".equals(res) ? "" : StringUtils.chop(res);
} public static String doPost(String url, Map<String, String> param, Map<String, String> requestHead) {
// 创建Httpclient对象
CloseableHttpClient httpClient = getHttpClient();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig());
if (null != requestHead) {
for (Map.Entry<String, String> entry : requestHead.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
httpPost.addHeader(key, value);
}
}
// 创建参数列表
if (param != null) {
List<NameValuePair> paramList = new ArrayList<NameValuePair>();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
httpPost.setEntity(entity);
}
// 执行http请求
response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), CHARSET_UTF_8);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
} public static String doPost(String url, Map<String, String> param) { return doPost(url, param, null);
} public static String doPost(String url) {
return doPost(url, null);
} }
package com.winner; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.util.HashMap;
import java.util.Map; public class RestClient { public static final String GET = "GET";
public static final String POST = "POST"; private static Logger logger = LoggerFactory.getLogger(RestClient.class); private static Map<String, String> requestHeaders = new HashMap<String, String>() {{
put("Content-Type", "application/json");
put("charset", "UTF-8");
}}; public static String sendData(String url, String method, String param, Map<String, String> headers) {
logger.info("调用接口:{},方法:{}", url, method);
logger.info("参数:{}", param);
String returnStr = ""; if (headers != null) {
for (Map.Entry<String, String> entry : requestHeaders.entrySet()) {
headers.put(entry.getKey(), entry.getValue());
}
} else {
headers = requestHeaders;
}
try {
if (method.equals(GET)) {
returnStr = HttpClientUtils.doGetJson(url, param, headers); }
if (method.equals(POST)) {
returnStr = HttpClientUtils.doPostJson(url, param, headers);
}
} catch (Exception exception) {
logger.error("系统调用异常:", exception);
//注意,在Controller中进行捕捉
throw new BusinessException(00000, exception.getMessage());
}
logger.info("调用完成,返回值:{}", returnStr);
return returnStr;
}
}