先自定义两个异常
public class ServiceRumtimeException extends RuntimeException {
private String code;
public ServiceRumtimeException(String code) {
this.code = code;
}
public ServiceRumtimeException(String code, String message) {
super(message);
this.code = code;
}
public String toString() {
return "ServiceRumtimeException(code=" + this.getCode() + ")";
}
public String getCode() {
return this.code;
}
}
/**
*
* 其他异常
*/
public class OtherRumtimeException extends RuntimeException {
public OtherRumtimeException(String message) {
super(message);
}
}
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
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.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import wwfww.warehouse.exceptions.OtherRumtimeException;
import wwfww.warehouse.exceptions.ServiceRumtimeException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.*;
/**
* http请求公共类
* 1. GET提交
* 2. POST FORM提交
* 3. POST JSON提交
*/
public class HttpClientUtil {
/**
* 日志
*/
private final static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
private static PoolingHttpClientConnectionManager connMgr;
private static RequestConfig requestConfig;
private static final int MAX_TIMEOUT = 120000;
private static CloseableHttpClient httpClient = null;
static {
// 设置连接池
connMgr = new PoolingHttpClientConnectionManager();
// 设置连接池大小
connMgr.setMaxTotal(100);
connMgr.setDefaultMaxPerRoute(connMgr.getMaxTotal());
RequestConfig.Builder configBuilder = RequestConfig.custom();
// 设置连接超时
configBuilder.setConnectTimeout(MAX_TIMEOUT);
// 设置读取超时
configBuilder.setSocketTimeout(MAX_TIMEOUT);
// 设置从连接池获取连接实例的超时
configBuilder.setConnectionRequestTimeout(MAX_TIMEOUT);
// 在提交请求之前 测试连接是否可用
configBuilder.setStaleConnectionCheckEnabled(true);
requestConfig = configBuilder.build();
}
/**
* 生产HttpClient实例
* 公开,静态的工厂方法,需要使用时才去创建该单体
*/
public static CloseableHttpClient getHttpClient() {
if (httpClient == null) {
httpClient = HttpClients.createDefault();
}
return httpClient;
}
/**
* GET请求 url为请求的路径 带参数
*
* @param url 请求路径
* @return String
*/
public static String doGet(String url) {
CloseableHttpResponse response = null;
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
try {
response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println(result);
return result;
}
/**
* 发送 POST 请求(HTTP),K-V形式
* Content-Type: application/x-www-form-urlencoded; charset=UTF-8
*
* @param apiUrl API接口URL(必填项)
* @param params 参数map
* @return HttpResponse对象
*/
public static String doPost(String apiUrl, Map<String, Object> params) {
String result = "";
CloseableHttpResponse response = null;
CloseableHttpClient httpClient = getHttpClient();
HttpPost httpPost = new HttpPost(apiUrl);
httpPost.setConfig(requestConfig);
List<NameValuePair> pairList = new ArrayList<NameValuePair>(params.size());
for (Map.Entry<String, Object> entry : params.entrySet()) {
if (entry.getValue() != null && !"".equals(entry.getValue())) {
NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
pairList.add(pair);
}
}
httpPost.setEntity(new UrlEncodedFormEntity(pairList, Charset.forName("UTF-8")));
try {
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
if (statusCode != HttpStatus.SC_OK) {
throw new ServiceRumtimeException("900", "请求发生异常");
}
} catch (IOException e) {
e.printStackTrace();
throw new ServiceRumtimeException("900", "请求发生异常");
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* POST请求
* Content Type: application/json
* 1. 接收json参数
* 2. 可传入header参数
*
* @param apiUrl
* @param jsonStr
* @return
*/
public static Object doPost(String apiUrl, String jsonStr, Map<String, Object> header) {
String result = "";
CloseableHttpResponse response = null;
CloseableHttpClient httpClient = getHttpClient();
HttpPost httpPost = new HttpPost(apiUrl);
httpPost.setConfig(requestConfig);
StringEntity req = new StringEntity(jsonStr, "utf-8");//解决中文乱码问题
req.setContentEncoding("UTF-8");
req.setContentType("application/json");
httpPost.setEntity(req);
httpPost.setHeader("Accept", "application/json");
//设置header参数
if (header != null) {
Iterator<String> iter = header.keySet().iterator();
while (iter.hasNext()) {
String key = iter.next();
httpPost.setHeader(key, header.get(key) == null ? "" : header.get(key).toString());
}
}
try {
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
System.out.println("statusCode=" + statusCode);
if (statusCode != HttpStatus.SC_OK) {
throw new ServiceRumtimeException("900", "请求发生异常");
}
} catch (IOException e) {
e.printStackTrace();
throw new ServiceRumtimeException("900", "请求发生异常");
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println(result);
return result;
}
/**
* GET请求 url为请求的路径 带header参数
*
* @param url 请求路径
* @return String
*/
public static String doGet(String url, Map<String, Object> headers) {
logger.info(String.format("get url:%s", url));
CloseableHttpResponse response = null;
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
// RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();
// httpGet.setConfig(requestConfig);
//设置header 参数
if (headers != null) {
Set<String> keys = headers.keySet();
for (Iterator<String> i = keys.iterator(); i.hasNext(); ) {
String key = (String) i.next();
httpGet.addHeader(key, headers.get(key).toString());
}
}
try {
response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "GBK");
logger.info(String.format("get url result:%s", result));
if (statusCode != HttpStatus.SC_OK) {
throw new ServiceRumtimeException("900", "请求发生异常"+url);
}
} catch (IOException e) {
logger.error("doGet",e);
throw new ServiceRumtimeException("900", "请求发生异常"+url);
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* 转换map成string key=value格式
*
* @param map
* @return
*/
public static String changeMap(Map<String, Object> map) {
if (map == null) {
return "";
}
StringBuilder sb = new StringBuilder();
Set<String> keys = map.keySet();
for (Iterator<String> i = keys.iterator(); i.hasNext(); ) {
if (StringUtils.isNotEmpty(sb.toString())) {
sb.append("&");
}
String key = i.next();
Object value = map.get(key);
if (value == null) {
throw new OtherRumtimeException(key+"参数为空");
}
sb.append(key).append("=").append(value);
}
return sb.toString();
}
/**
* 接口返回文件流生成文件
* @param url 接口地址
* @param path 存文件的绝对路径(比如:d://xxx.pdf)
*/
static public void getStreamToFile(String url,String path) {
// 创建默认连接
CloseableHttpClient httpClient =null;
CloseableHttpResponse response =null;
InputStream is =null;
FileOutputStream fos =null;
try {
httpClient = HttpClients.createDefault();
// 创建get请求
HttpGet httpGet = new HttpGet(url);
response = httpClient.execute(httpGet);
System.out.println(response);
int code = response.getStatusLine().getStatusCode();
// 获取状态
if (code == 200) {
HttpEntity entity = response.getEntity();
is = entity.getContent();
fos = new FileOutputStream(path);
byte[] b = new byte[1024];
int length;
while((length= is.read(b))!= -1){
fos.write(b,0,length);
}
}
} catch (IOException e) {
logger.error("流转换异常");
} finally {
if (is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (response!=null){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (httpClient!=null){
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}