1、添加依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
2、HttpClientUtil工具类
package com.task.test.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
@Slf4j
public class HttpClientUtil {
public static String doGet(String url) {
CloseableHttpClient closeableHttpClient = null;
HttpGet httpGet = null;
String respone = null;
try{
closeableHttpClient = HttpClientBuilder.create().build();
RequestConfig build = RequestConfig.custom().setConnectionRequestTimeout(10000).setConnectTimeout(10000).setSocketTimeout(10000).build();
httpGet = new HttpGet(url);
httpGet.setConfig(build);
CloseableHttpResponse execute = closeableHttpClient.execute(httpGet);
if(execute!=null){
HttpEntity entity = execute.getEntity();
if(entity!=null){
respone = EntityUtils.toString(entity,"UTF-8");
}
}
execute.close();
}catch (Exception e){
log.info("请求接口url:{},异常信息:{}",url,e.getMessage());
e.printStackTrace();
}finally {
try{
closeableHttpClient.close();
}catch (Exception e){
e.getMessage();
}
}
return respone;
}
public static String doPost(String url,String context){
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = HttpClientBuilder.create().build();
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(10000).setSocketTimeout(10000).setConnectTimeout(10000).build();
httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
StringEntity stringEntity = new StringEntity(context,"utf-8");
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
CloseableHttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,"UTF-8");
}
}
response.close();
System.out.print(httpClient);
}catch(Exception ex){
log.info("请求接口url:{},异常信息:{}",url,ex.getMessage());
ex.printStackTrace();
} finally{
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}
3、Get请求
public static final String ACCESS_TOKEN_URL = "http://localhost:9090/crud/list?name=NAME&classId=CLASSID"; @GetMapping("/list") public String list() throws IOException { String replace = ACCESS_TOKEN_URL.replace("NAME", "王五").replace("CLASSID", "3"); String result = HttpClient.doGet(replace); return result; }
4、Post请求
@PostMapping("/add") public String add(@RequestBody TbTemplatePublic tbTemplatePublic){ JSONObject jsonObject = new JSONObject(); jsonObject.put("templateType", tbTemplatePublic.getTemplateType()); jsonObject.put("policeCode", tbTemplatePublic.getPoliceCode()); jsonObject.put("templateContent", tbTemplatePublic.getTemplateContent()); jsonObject.put("templateTitle", tbTemplatePublic.getTemplateTitle()); jsonObject.put("fraudType", tbTemplatePublic.getFraudType()); String result = HttpClientUtil.doPost("http://localhost:12006/templatePublic/add", jsonObject.toJSONString()); return result; }
如果写的有不对的地方,请大家指出来,我们一起进步,谢谢。