<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);
}
}