<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
/**
* @ClassName HttpClient
* @Author sys
* @Date 2021/12/23 15:07
* @Description //请求工具类
* @Version
**/
@Slf4j
public class HttpClient {
/**
* GET---无参测试
*
* @date
*/
public static String doGet(String url,String token) {
// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 创建Get请求
HttpGet httpGet = new HttpGet(url);
// 响应模型
CloseableHttpResponse response = null;
try {
if(org.apache.commons.lang3.StringUtils.isNotEmpty(token)){
httpGet.setHeader("Authorization","Bearer "+token);
}
// 由客户端执行(发送)Get请求
response = httpClient.execute(httpGet);
// 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
log.info("响应状态为:{}",response.getStatusLine());
if (responseEntity != null) {
String s = EntityUtils.toString(responseEntity) ;
log.info("响应内容:{}",s);
return s;
}
} catch (Exception e) {
System.out.println("连接失败" + e.getMessage());
log.error("连接失败",e);
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* POST---有参测试(普通参数)
*
* @date
*/
public static String doPost(String url, Map<String, Object> map,String jsonString,String token) {
// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 参数
StringBuilder params = new StringBuilder();
try {
if (map != null && map.size() > 0) {
// 字符数据最好encoding以下;这样一来,某些特殊字符才能传过去(如:某人的名字就是“&”,不encoding的话,传不过去)
for(Map.Entry e:map.entrySet()){
params.append(e.getKey()).append(URLEncoder.encode(String.valueOf(e.getValue()), "utf-8"));
params.append("&");
}
url = url + "?" + params ;
}
} catch (Exception e1) {
e1.printStackTrace();
}
// 创建Post请求
HttpPost httpPost = new HttpPost(url);
if(!StringUtils.isEmpty(jsonString)) {
StringEntity entity = new StringEntity(jsonString, "UTF-8");
// post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
httpPost.setEntity(entity);
}
// 设置ContentType(注:如果只是传普通参数的话,ContentType不一定非要用application/json)
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
if(org.apache.commons.lang3.StringUtils.isNotEmpty(token)){
httpPost.setHeader("Authorization", "Bearer "+token);
}
// 响应模型
CloseableHttpResponse response = null;
try {
// 由客户端执行(发送)Post请求
response = httpClient.execute(httpPost);
// 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
log.info("响应状态为:{}",response.getStatusLine());
if (responseEntity != null ) {
System.out.println("响应内容长度为:" + responseEntity.getContentLength());
String str = EntityUtils.toString(responseEntity) ;
log.info("响应:{}",str);
return str;
}
} catch (Exception e) {
e.printStackTrace();
log.error("http请求错误",e);
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return "";
}
/**
* POST---有参测试(普通参数)
*
* @date
*/
public static String doPostV2(String url, Map<String, Object> parms,String dataString,String token) {
// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 参数
StringBuilder params = new StringBuilder();
try {
if (parms != null && parms.size() > 0) {
// 字符数据最好encoding以下;这样一来,某些特殊字符才能传过去(如:某人的名字就是“&”,不encoding的话,传不过去)
for(Map.Entry e:parms.entrySet()){
params.append(e.getKey()).append(URLEncoder.encode(String.valueOf(e.getValue()), "utf-8"));
params.append("&");
}
url = url + "?" + params ;
}
} catch (Exception e1) {
e1.printStackTrace();
}
// 创建Post请求
HttpPost httpPost = new HttpPost(url);
if(!StringUtils.isEmpty(dataString)) {
StringEntity entity = new StringEntity(dataString, "UTF-8");
// post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
httpPost.setEntity(entity);
}
// 设置ContentType(注:如果只是传普通参数的话,ContentType不一定非要用application/json)
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
if(org.apache.commons.lang3.StringUtils.isNotEmpty(token)){
httpPost.setHeader("Authorization", token);
}
// 响应模型
CloseableHttpResponse response = null;
try {
// 由客户端执行(发送)Post请求
response = httpClient.execute(httpPost);
// 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
log.info("响应状态为:{}",response.getStatusLine());
if (responseEntity != null ) {
String str = EntityUtils.toString(responseEntity) ;
log.info("响应:{}",str);
return str;
}
} catch (Exception e) {
e.printStackTrace();
log.error("http请求错误",e);
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return "";
}
/**
* POST---有参测试(普通参数)
*
* @date
*/
public static String doPostV3(String url, Map<String, Object> parms,String dataString,String tokenKey,String tokenV) {
// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 参数
StringBuilder params = new StringBuilder();
try {
if (parms != null && parms.size() > 0) {
// 字符数据最好encoding以下;这样一来,某些特殊字符才能传过去(如:某人的名字就是“&”,不encoding的话,传不过去)
for(Map.Entry e:parms.entrySet()){
params.append(e.getKey()).append(URLEncoder.encode(String.valueOf(e.getValue()), "utf-8"));
params.append("&");
}
url = url + "?" + params ;
}
} catch (Exception e1) {
e1.printStackTrace();
}
// 创建Post请求
HttpPost httpPost = new HttpPost(url);
if(!StringUtils.isEmpty(dataString)) {
StringEntity entity = new StringEntity(dataString, "UTF-8");
// post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
httpPost.setEntity(entity);
}
// 设置ContentType(注:如果只是传普通参数的话,ContentType不一定非要用application/json)
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
if(org.apache.commons.lang3.StringUtils.isNotEmpty(tokenV)){
httpPost.setHeader(tokenKey, tokenV);
}
// 响应模型
CloseableHttpResponse response = null;
try {
// 由客户端执行(发送)Post请求
response = httpClient.execute(httpPost);
// 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
log.info("响应状态为:{}",response.getStatusLine());
if (responseEntity != null ) {
String str = EntityUtils.toString(responseEntity) ;
log.info("响应:{}",str);
return str;
}
} catch (Exception e) {
e.printStackTrace();
log.error("http请求错误",e);
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return "";
}
public static void main(String[] args) {
HttpClient httpClient = new HttpClient();
httpClient.doPost("https://www.jianshu.com/p/6baff39af368",null,null,null);
}
}