在开发项目时,经常需要对接第三方平台接口,而且是后台触发请求,比如对接短信平台接口、语音中心平台接口、支付平台接口等等,可以引入commons-httpclient方式进行开发。
1、在pom中添加依赖
<!-- 工具类 -->
<org.apache.commons.lang3>3.4</org.apache.commons.lang3>
<http.client.version>3.1</http.client.version>
<!-- http -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>${http.client.version}</version>
</dependency>
<!-- 针对java.lang的工具 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${org.apache.commons.lang3}</version>
</dependency>
2、POST请求方法
/**
* 无 header 参数
* @param url
* @param param
* @param charset
* @param contentType
* @return
*/
public static String sendPost(String url, String param, String charset, String contentType) {
String reqCharset = StringUtility.isEmpty(charset) ? CHARSET_UTF8 : charset;
String type = StringUtility.isEmpty(contentType) ? CONTENT_TYPE_JSON : contentType;
String result = StringUtils.EMPTY;
PostMethod method = null;
try {
HttpClient client = new HttpClient();
//设置连接超时时间为30秒(连接初始化时间)
client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
//带参数的那个请求的访问串
method = new PostMethod(url);
// 发送请求
//指定传送字符集为UTF-8格式
RequestEntity requestEntity = new StringRequestEntity(param, type, reqCharset);
method.setRequestEntity(requestEntity);
int statusCode = client.executeMethod(method);
if (statusCode == 200) {
//解析返回支付Json数据
result = method.getResponseBodyAsString();
}
// 关闭连接
client.getHttpConnectionManager().closeIdleConnections(1);
return result;
} catch (Exception e) {
return StringUtils.EMPTY;
} finally {
if (method != null) {
// 释放连接
method.releaseConnection();
}
}
}
/**
* 带 header 参数
* @param url
* @param param
* @param map
* @param charset
* @param contentType
* @return
*/
public static String sendHeaderPost(String url, String param, Map<String, String> map, String charset, String contentType) {
String reqCharset = StringUtility.isEmpty(charset) ? CHARSET_UTF8 : charset;
String type = StringUtility.isEmpty(contentType) ? CONTENT_TYPE_JSON : contentType;
String result = StringUtils.EMPTY;
PostMethod method = null;
try {
HttpClient client = new HttpClient();
// 设置连接超时时间为30秒(连接初始化时间)
client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
// 带参数的那个请求的访问串
method = new PostMethod(url);
// 设置消息头
if (CollectionUtility.isNotEmpty(map)) {
for (Map.Entry<String, String> entry : map.entrySet()) {
method.addRequestHeader(entry.getKey(), entry.getValue());
}
}
// 发送请求 指定传送字符集为UTF-8格式
RequestEntity requestEntity = new StringRequestEntity(param, type, reqCharset);
method.setRequestEntity(requestEntity);
int statusCode = client.executeMethod(method);
if (statusCode == 200) {
//解析返回支付Json数据
result = method.getResponseBodyAsString();
}
// 关闭连接
client.getHttpConnectionManager().closeIdleConnections(1);
return result;
} catch (Exception e) {
return StringUtils.EMPTY;
} finally {
if (method != null) {
// 释放连接
method.releaseConnection();
}
}
}
3、完整的工具类
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.lang.StringUtils;
import java.util.Map;
/**
* http调用POST请求
*/
public class HttpClientUtility {
static final String CHARSET_UTF8 = "utf-8";
static final String CONTENT_TYPE_JSON = "application/json";
/**
* 默认编码utf-8,content-type为application/json
* @param url
* @param param
* @return
*/
public static String sendPost(String url, String param) {
return sendPost(url, param, CHARSET_UTF8, CONTENT_TYPE_JSON);
}
/**
* 默认编码utf-8 指定content-type为application/json
* @param url
* @param param
* @param contentType
* @return
*/
public static String sendPost(String url, String param, String contentType) {
String type = StringUtility.isEmpty(contentType) ? CONTENT_TYPE_JSON : contentType;
return sendPost(url, param, CHARSET_UTF8, type);
}
/**
* 指定header参数
* @param url
* @param param
* @param map
* @param contentType
* @return
*/
public static String sendHeaderPost(String url, String param, Map<String, String> map, String contentType) {
String type = StringUtility.isNotEmpty(contentType) ? CONTENT_TYPE_JSON : contentType;
return sendHeaderPost(url, param, map, CHARSET_UTF8, type);
}
/**
* 无 header 参数
* @param url
* @param param
* @param charset
* @param contentType
* @return
*/
public static String sendPost(String url, String param, String charset, String contentType) {
String reqCharset = StringUtility.isEmpty(charset) ? CHARSET_UTF8 : charset;
String type = StringUtility.isEmpty(contentType) ? CONTENT_TYPE_JSON : contentType;
String result = StringUtils.EMPTY;
PostMethod method = null;
try {
HttpClient client = new HttpClient();
//设置连接超时时间为30秒(连接初始化时间)
client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
//带参数的那个请求的访问串
method = new PostMethod(url);
// 发送请求
//指定传送字符集为UTF-8格式
RequestEntity requestEntity = new StringRequestEntity(param, type, reqCharset);
method.setRequestEntity(requestEntity);
int statusCode = client.executeMethod(method);
if (statusCode == 200) {
//解析返回支付Json数据
result = method.getResponseBodyAsString();
}
// 关闭连接
client.getHttpConnectionManager().closeIdleConnections(1);
return result;
} catch (Exception e) {
return StringUtils.EMPTY;
} finally {
if (method != null) {
// 释放连接
method.releaseConnection();
}
}
}
/**
* 带 header 参数
* @param url
* @param param
* @param map
* @param charset
* @param contentType
* @return
*/
public static String sendHeaderPost(String url, String param, Map<String, String> map, String charset, String contentType) {
String reqCharset = StringUtility.isEmpty(charset) ? CHARSET_UTF8 : charset;
String type = StringUtility.isEmpty(contentType) ? CONTENT_TYPE_JSON : contentType;
String result = StringUtils.EMPTY;
PostMethod method = null;
try {
HttpClient client = new HttpClient();
// 设置连接超时时间为30秒(连接初始化时间)
client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
// 带参数的那个请求的访问串
method = new PostMethod(url);
// 设置消息头
if (CollectionUtility.isNotEmpty(map)) {
for (Map.Entry<String, String> entry : map.entrySet()) {
method.addRequestHeader(entry.getKey(), entry.getValue());
}
}
// 发送请求 指定传送字符集为UTF-8格式
RequestEntity requestEntity = new StringRequestEntity(param, type, reqCharset);
method.setRequestEntity(requestEntity);
int statusCode = client.executeMethod(method);
if (statusCode == 200) {
//解析返回支付Json数据
result = method.getResponseBodyAsString();
}
// 关闭连接
client.getHttpConnectionManager().closeIdleConnections(1);
return result;
} catch (Exception e) {
return StringUtils.EMPTY;
} finally {
if (method != null) {
// 释放连接
method.releaseConnection();
}
}
}
}