1、maven引入
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.4</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.5</version> </dependency>
2、封裝post請求方法
public static String httpPost(String url,Map map){
// 返回body
String body = null;
// 獲取連接客戶端工具
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse=null;
// 2、創建一個HttpPost請求
HttpPost post = new HttpPost(url);
// 5、設置header信息
/**header中通用屬性*/
post.setHeader("Accept","*/*");
post.setHeader("Accept-Encoding","gzip, deflate");
post.setHeader("Cache-Control","no-cache");
post.setHeader("Connection", "keep-alive");
post.setHeader("Content-Type", "application/json;charset=UTF-8");
/**業務參數*/
post.setHeader("test1","test1");
post.setHeader("test2",2);
// 設置參數
if (map != null) {
//System.out.println(JSON.toJSONString(map));
try {
StringEntity entity1=new StringEntity(JSON.toJSONString(map),"UTF-8");
entity1.setContentEncoding("UTF-8");
entity1.setContentType("application/json");
post.setEntity(entity1);
// 7、執行post請求操作,並拿到結果
httpResponse = httpClient.execute(post);
// 獲取結果實體
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
// 按指定編碼轉換結果實體為String類型
body = EntityUtils.toString(entity, "UTF-8");
}
try {
httpResponse.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return body;
}
3、封裝GET請求方法
public static String httpGet(String url){
// 獲取連接客戶端工具
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse=null;
String finalString = null;
HttpGet httpGet = new HttpGet(url);
/**公共參數添加至httpGet*/
/**header中通用屬性*/
httpGet.setHeader("Accept","*/*");
httpGet.setHeader("Accept-Encoding","gzip, deflate");
httpGet.setHeader("Cache-Control","no-cache");
httpGet.setHeader("Connection", "keep-alive");
httpGet.setHeader("Content-Type", "application/json;charset=UTF-8");
/**業務參數*/
httpGet.setHeader("test1","test1");
httpGet.setHeader("test2",2);
try {
httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
finalString= EntityUtils.toString(entity, "UTF-8");
try {
httpResponse.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return finalString;
}
4、封裝put請求
public static String httpPut(String url,Map map){
String encode = "utf-8";
//HttpClients.createDefault()等價於 HttpClientBuilder.create().build();
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
HttpPut httpput = new HttpPut(url);
// 5、設置header信息
/**header中通用屬性*/
httpput.setHeader("Accept","*/*");
httpput.setHeader("Accept-Encoding","gzip, deflate");
httpput.setHeader("Cache-Control","no-cache");
httpput.setHeader("Connection", "keep-alive");
httpput.setHeader("Content-Type", "application/json;charset=UTF-8");
/**業務參數*/
httpput.setHeader("test1","test1");
httpput.setHeader("test2",2);
//組織請求參數
StringEntity stringEntity = new StringEntity(JSON.toJSONString(map), encode);
httpput.setEntity(stringEntity);
String content = null;
CloseableHttpResponse httpResponse = null;
try {
//響應信息
httpResponse = closeableHttpClient.execute(httpput);
HttpEntity entity = httpResponse.getEntity();
content = EntityUtils.toString(entity, encode);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
httpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
closeableHttpClient.close(); //關閉連接、釋放資源
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
5、封裝delete請求
public static String httpDelete(String url){
//HttpResponse response = new HttpResponse();
String encode = "utf-8";
String content = null;
//since 4.3 不再使用 DefaultHttpClient
CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
HttpDelete httpdelete = new HttpDelete(url);
// 5、設置header信息
/**header中通用屬性*/
httpdelete.setHeader("Accept","*/*");
httpdelete.setHeader("Accept-Encoding","gzip, deflate");
httpdelete.setHeader("Cache-Control","no-cache");
httpdelete.setHeader("Connection", "keep-alive");
httpdelete.setHeader("Content-Type", "application/json;charset=UTF-8");
/**業務參數*/
httpdelete.setHeader("test1","test1");
httpdelete.setHeader("test2",2);
CloseableHttpResponse httpResponse = null;
try {
httpResponse = closeableHttpClient.execute(httpdelete);
HttpEntity entity = httpResponse.getEntity();
content = EntityUtils.toString(entity, encode);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
httpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try { //關閉連接、釋放資源
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
