1.使用apache HttpClient发送Http请求
首先需要添加依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
以上是我使用的springboot版本,如果项目的父pom也是使用的springboot,那么以下依赖不需要指定版本,版本由springboot指定就可以了
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<!--<version>4.5.13</version>--> 版本springboot指定
</dependency>
以下是使用一个调用发送邮件的接口来做测试()
@Transactional(rollbackFor = Exception.class)
public String endTask() {
String url = "xxx.cn";//某接口url
String to = "xxx@qq.com, xxxxx@qq.com, xxxxx@qq.com";
String sender = xxxxxds@qq.com;
String respCtn = "";
// 1、创建 client 实例
CloseableHttpClient httpclient = HttpClients.createDefault();
// 2、创建 post 实例
HttpPost post = new HttpPost(url);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("content", "测试邮件的内容");
params.add(new BasicNameValuePair("subject", "Exception Email"));//标题没有编码设置,所以最好设置英文,不然乱码
params.add(new BasicNameValuePair("to", to));
params.add(new BasicNameValuePair("sender", sender));
post.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8));
HttpResponse response = null;
try {
if (params != null)
// 3、设置 post 参数
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
// 4、发送请求
response = httpclient.execute(post);
respCtn = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
log.error("Fail to connect to remote host [" + url + "]" + e);
} finally {
if (httpclient != null) {
try {
httpclient.close();
} catch (Exception e) {
}
}
}
return respCtn;
}
2.使用RestTemplate发送Http请求
需要在springboot启动类同路径下生成RestTemplateConfig 类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(15000);
factory.setReadTimeout(5000);
return factory;
}
}
以下是调用代码:
@Autowired
private RestTemplate restTemplate;
public String xxx() {
RestTemplate restTemplate2 = new RestTemplate();
String url = "http://127.0.0.1:8080/xxx";
// 封装参数,这里是HashMap
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("变量1", "xxx");
paramMap.put("变量2", "xxx");
//1、使用getForObject请求接口
String result1 = template.getForObject(url, String.class, paramMap);
System.out.println("result1====================" + result1);
//2、使用exchange请求接口
HttpHeaders headers = new HttpHeaders();
headers.set("id", "lidy");
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(null,headers);
ResponseEntity<String> response2 = template.exchange(url, HttpMethod.GET, httpEntity, String.class,paramMap);
System.out.println("result2====================" + response2.getBody());
}
}