方式一:通过CloseableHTTPClient发送HttpGet和HttpPost请求,去掉日志可以直接使用
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
* @description 发送http请求工具类
* @author ykzhao
* @create 2020-2-25
*/
public class HttpRequestUtil {
/**
* @description 发送post请求
* @author ykzhao
* @create 2020-2-25
* @param url:接口地址
* @param header请求头参数
* @param params接口请求参数
* @throws Exception
*/
public static Object sendPostRequest(String url, Map<String, String> header, Map<String, String> params)
throws Exception {
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
try {
client = HttpClients.createDefault();
// 创建http请求
HttpPost httpPost = new HttpPost(url);
// 设置header参数
for (Map.Entry<String, String> entry : header.entrySet()) {
httpPost.setHeader(new BasicHeader(entry.getKey(), entry.getValue()));
}
// 设置请求参数
httpPost.setEntity(new UrlEncodedFormEntity(createParam(params), Consts.UTF_8));
// 发送请求
response = client.execute(httpPost);
// 获取返回结果
HttpEntity entity = response.getEntity();
String str = EntityUtils.toString(entity, "UTF-8");
return str;
} catch (Exception e) {
throw e;
} finally {
// 关闭资源
if (response != null) {
try {
response.close();
} catch (Exception e) {
_log.error("关闭response异常:" + e);
throw e;
}
}
if (client != null) {
try {
client.close();
} catch (Exception e) {
_log.error("关闭client异常:" + e);
throw e;
}
}
}
}
/**
* @description 发送post请求
* @author ykzhao
* @create 2020-2-25
* @param url:接口地址
* @param header请求头参数
* @param params接口请求参数
* @throws Exception
*/
public static Object sendGetRequest(String url, Map<String, String> header) throws Exception {
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
try {
client = HttpClients.createDefault();
URIBuilder uriBuilder = new URIBuilder(url);
HttpGet httpGet = new HttpGet(uriBuilder.build());
// 设置header参数
for (Map.Entry<String, String> entry : header.entrySet()) {
httpGet.setHeader(new BasicHeader(entry.getKey(), entry.getValue()));
}
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
return result;
} catch (Exception e) {
throw e;
} finally {
// 关闭资源
if (response != null) {
try {
response.close();
} catch (Exception e) {
_log.error("关闭response异常:" + e);
throw e;
}
}
if (client != null) {
try {
client.close();
} catch (Exception e) {
_log.error("关闭client异常:" + e);
throw e;
}
}
}
}
/**
* @description BasicNameValuePair通常是用来封装post请求中的参数名称和值
* @author ykzhao
* @create 2020-2-25
*/
private static List<NameValuePair> createParam(Map<String, String> params) {
// 建立一个NameValuePair数组,用于存储欲传送的参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if (params != null) {
for (String k : params.keySet()) {
nvps.add(new BasicNameValuePair(k, params.get(k).toString()));
}
}
return nvps;
}
}