maven包:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.8</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.10</version>
</dependency>
完成代码文件下载地址:
链接:https://pan.baidu.com/s/1fdeID83_ma_Fe3iPiW411g?pwd=1234
提取码:1234
/**
* Http协议Get请求
*
* @param url url带参数
* @param headers headers值
* @return get结果
*/
public static String httpGet(String url, Map<String, String> headers) {
CloseableHttpResponse response = null;
String result = "";
try {
httpClient = createHttpClient();
HttpGet httpGet = new HttpGet(url);
//设置header
setHeaders(httpGet, headers);
//发起请求
response = httpClient.execute(httpGet);
//处理返回结果
if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 300) {
log.error("httpGet请求异常,错误码:{}", response.getStatusLine().getStatusCode());
} else {
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, "UTF-8");
}
//关闭响应流
EntityUtils.consume(entity);
// httpGet.releaseConnection();
}
} catch (Exception e) {
log.error("httpGet异常", e);
} finally {
close(response);
}
return result;
}
/**
* Http协议Post json请求
*
* @param url url
* @param json json字符串
* @param headers headers值
* @return post结果
*/
public static String httpPostJson(String url, String json, Map<String, String> headers) {
CloseableHttpResponse response = null;
String result = "";
try {
httpClient = createHttpClient();
HttpPost httpPost = new HttpPost(url);
//设置header
httpPost.setHeader("Content-Type", "application/json");
setHeaders(httpPost, headers);
//填充参数
httpPost.setEntity(new StringEntity(json, "UTF-8"));// 解决中文乱码问题
//发起请求
response = httpClient.execute(httpPost);
//处理返回结果
if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 300) {
log.error("httpPostJson请求异常,错误码:{}", response.getStatusLine().getStatusCode());
} else {
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, "UTF-8");
}
//关闭响应流
EntityUtils.consume(entity);
}
} catch (Exception e) {
log.error("httpPostJson异常", e);
} finally {
close(response);
}
return result;
}
/**
* Http协议Post form请求
*
* @param url url
* @param paramMap <String, Object>
* @param headers headers值
* @return post结果
*/
public static String httpPostForm(String url, Map<String, Object> paramMap, Map<String, String> headers) {
CloseableHttpResponse response = null;
String result = "";
try {
httpClient = createHttpClient();
HttpPost httpPost = new HttpPost(url);
//设置header
setHeaders(httpPost, headers);
//填充参数
if (paramMap != null) {
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
list.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
// 构造from表单对象
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "UTF-8");
// 把表单放到post里
httpPost.setEntity(urlEncodedFormEntity);
}
//发起请求
response = httpClient.execute(httpPost);
//处理返回结果
if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 300) {
log.error("httpPostForm请求异常,错误码:{}", response.getStatusLine().getStatusCode());
} else {
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, "UTF-8");
}
//关闭响应流
EntityUtils.consume(entity);
}
} catch (Exception e) {
log.error("httpPostForm异常", e);
} finally {
close(response);
}
return result;
}
/**
* Http协议Put请求
*
* @param url url
* @param json json字符串
* @param headers headers值
* @return put结果
*/
public static String httpPut(String url, String json, Map<String, String> headers) {
CloseableHttpResponse response = null;
String result = "";
try {
httpClient = createHttpClient();
HttpPut httpPut = new HttpPut(url);
//设置header
httpPut.setHeader("Content-Type", "application/json");
setHeaders(httpPut, headers);
//填充参数
httpPut.setEntity(new StringEntity(json, "UTF-8"));// 解决中文乱码问题
//发起请求
response = httpClient.execute(httpPut);
//处理返回结果
if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 300) {
log.error("httpPut请求异常,错误码:{}", response.getStatusLine().getStatusCode());
} else {
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, "UTF-8");
}
//关闭响应流
EntityUtils.consume(entity);
}
} catch (Exception e) {
log.error("httpPut异常", e);
} finally {
close(response);
}
return result;
}