public class HttpclientUtils {
@Autowired(required = false)
private CloseableHttpClient httpClient;
@Autowired(required = false)
private RequestConfig config;
/**
* 没有带参数GET请求
*
* @param url 请求地址
* @return 返回状态码为200 返回页面内容 否则,返回null
* @throws Exception
*/
public String doget(String url) throws Exception {
// 创建http GET请求
HttpGet httpGet = new HttpGet(url);
// 设置请求参数
httpGet.setConfig(config);
CloseableHttpResponse response = null;
try {
// 执行请求
response = httpClient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
return content;
}
} finally {
if (response != null) {
response.close();
}
// httpClient.close();
}
return null;
}
/**
* 带有参数GET请求
*
* @param url 请求地址
* @param params 请求参数
* @return 返回状态码为200 返回页面内容 否则,返回null
* @throws Exception
*/
public String doget(String url, Map<String, Object> params) throws Exception {
// 定义请求的参数
URIBuilder uriBuilder = new URIBuilder(url);
if (!params.isEmpty()) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
}
}
URI uri = uriBuilder.build();
// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
// 设置请求参数
httpGet.setConfig(config);
CloseableHttpResponse response = null;
try {
// 执行请求
response = httpClient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
return EntityUtils.toString(response.getEntity(), "UTF-8");
}
} finally {
if (response != null) {
response.close();
}
// httpclient.close();
}
return null;
}
/**
* 带有参数的POST
* @param url 请求地址
* @param params 请求参数
* @return 返回状态码为200 HttpResut(200,内容) 否则,返回HttpResut(当前状态码,null)
* @throws Exception
*/
public HttpResut dopost(String url, Map<String, Object> params) throws Exception {
// 创建http POST请求
HttpPost httpPost = new HttpPost(url);
// 设置2个post参数,一个是scope、一个是q
List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
if (!params.isEmpty()) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
}
// 构造一个form表单式的实体
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters, "UTF-8");
// 将请求实体设置到httpPost对象中
httpPost.setEntity(formEntity);
// 设置请求参数
httpPost.setConfig(config);
CloseableHttpResponse response = null;
try {
// 执行请求
response = httpClient.execute(httpPost);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
return new HttpResut(response.getStatusLine().getStatusCode(), EntityUtils.toString(
response.getEntity(), "UTF-8"));
}
} finally {
if (response != null) {
response.close();
}
// httpclient.close();
}
return new HttpResut(response.getStatusLine().getStatusCode(), null);
}
/**
* 没有带参数POST请求
* @throws Exception
*/
public HttpResut dopost(String url) throws Exception{
return this.dopost(url, new HashMap<String, Object>());
}
//doput dodelete dopostjson ...
/**
* 指定POST请求
*
* @param url
* @param param 请求参数
* @return 状态码和请求的body
* @throws IOException
*/
public HttpResut doPostJson(String url, String json) throws IOException {
// 创建http POST请求
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(this.config);
if (json != null) {
// 构造一个字符串的实体
StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
// 将请求实体设置到httpPost对象中
httpPost.setEntity(stringEntity);
}
CloseableHttpResponse response = null;
try {
// 执行请求
response = httpClient.execute(httpPost);
if (response.getEntity() != null) {
return new HttpResut(response.getStatusLine().getStatusCode(), EntityUtils.toString(
response.getEntity(), "UTF-8"));
}
return new HttpResut(response.getStatusLine().getStatusCode(), null);
} finally {
if (response != null) {
response.close();
}
}
}
/**
* 执行PUT请求
*
* @param url
* @param param 请求参数
* @return 状态码和请求的body
* @throws IOException
*/
public HttpResut doPut(String url, Map<String, Object> param) throws IOException {
// 创建http POST请求
HttpPut httpPut = new HttpPut(url);
httpPut.setConfig(config);
if (param != null) {
// 设置post参数
List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
for (Map.Entry<String, Object> entry : param.entrySet()) {
parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
// 构造一个form表单式的实体,并且指定参数的编码为UTF-8
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters, "UTF-8");
// 将请求实体设置到httpPost对象中
httpPut.setEntity(formEntity);
}
CloseableHttpResponse response = null;
try {
// 执行请求
response = httpClient.execute(httpPut);
if (response.getEntity() != null) {
return new HttpResut(response.getStatusLine().getStatusCode(), EntityUtils.toString(
response.getEntity(), "UTF-8"));
}
return new HttpResut(response.getStatusLine().getStatusCode(), null);
} finally {
if (response != null) {
response.close();
}
}
}
/**
* 执行PUT请求
*
* @param url
* @return 状态码和请求的body
* @throws IOException
*/
public HttpResut doPut(String url) throws IOException {
return this.doPut(url, null);
}
/**
* 执行DELETE请求,通过POST提交,_method指定真正的请求方法
*
* @param url
* @param param 请求参数
* @return 状态码和请求的body
* @throws IOException
*/
public HttpResut doDelete(String url, Map<String, Object> param) throws Exception {
param.put("_method", "DELETE");
return this.dopost(url, param);
}
/**
* 执行DELETE请求(真正的DELETE请求)
*
* @param url
* @return 状态码和请求的body
* @throws IOException
*/
public HttpResut doDelete(String url) throws Exception {
// 创建http DELETE请求
HttpDelete httpDelete = new HttpDelete(url);
httpDelete.setConfig(this.config);
CloseableHttpResponse response = null;
try {
// 执行请求
response = httpClient.execute(httpDelete);
if (response.getEntity() != null) {
return new HttpResut(response.getStatusLine().getStatusCode(), EntityUtils.toString(
response.getEntity(), "UTF-8"));
}
return new HttpResut(response.getStatusLine().getStatusCode(), null);
} finally {
if (response != null) {
response.close();
}
}
}
}